import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from scipy import stats
import seaborn as sns
#WTI #Brent #Eternal #Gasoline
oil_price = pd.read_csv('Weekly_data_csv_v4.csv')
oil_price.columns
oil_price = oil_price[['Weekindex', 'Date', 'Year', 'Month', 'Week', 'Day', 'Par_Month','Par_Week','Par_Day', 'oil_spot_WTI','oil_spot_Brent','future_oil_contract_1','sugar_price_us', 'ethanol_price_us','chg_oil_spot_WTI','chg_oil_spot_Brent','chg_oil_spot_WTI_Positive','chg_oil_spot_WTI_Negative','chg_oil_future_contract1','chg_oil_future_contract1_Positive','chg_oil_future_contract1_Negative','Gasoline_Price_US','chg_Gasoline_price_US','chg_Gasoline_price_US_Positive','chg_Gasoline_price_US_future']]
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import matplotlib.cbook as cbook
from datetime import datetime, date
from dateutil.relativedelta import relativedelta
oil_price['Date_edited'] = oil_price['Date'].apply(lambda x : datetime.strptime( x,'%m/%d/%Y') )
years = mdates.YearLocator() # every year
months = mdates.MonthLocator() # every month
years_fmt = mdates.DateFormatter('%Y')
fig, ax = plt.subplots(figsize = (15,8))
ax.plot('Date_edited', 'oil_spot_WTI', data=oil_price, color = 'tab:blue')
# format the ticks
ax.xaxis.set_major_locator(years)
ax.xaxis.set_major_formatter(years_fmt)
ax.xaxis.set_minor_locator(months)
# round to nearest years.
datemin = np.datetime64(list(oil_price['Date_edited'])[0].strftime('%Y-%m-%d'),'Y')
datemax = np.datetime64(list(oil_price['Date_edited'])[-1].strftime('%Y-%m-%d'),'Y') + np.timedelta64(1, 'Y')
#datemin = datetime.strptime(list(oil_price['Date'])[0],'%m/%d/%Y')
#datemax = datetime.strptime(list(oil_price['Date'])[-1],'%m/%d/%Y') + relativedelta(years=1)
ax.set_xlim(datemin, datemax)
# format the coords message box
ax.format_xdata = mdates.DateFormatter('%Y-%m-%d')
ax.format_ydata = lambda x: '$%1.2f' % x # format the price.
ax.grid(True)
ax.set_xlabel('Year',fontdict={'fontsize': 15, 'fontweight': 'medium'})
ax.set_ylabel('Oil spot WTI',fontdict={'fontsize': 15, 'fontweight': 'medium'})
ax.set_title('Change in oil price since 1991 ', fontdict={'fontsize': 20, 'fontweight': 'medium'})
ax2 = ax.twinx() # instantiate a second axes that shares the same x-axis
#color = color = 'tab:blue'
ax2.plot('Date_edited', 'chg_oil_spot_WTI', data=oil_price, color = 'tab:grey', alpha=0.2)
ax2.tick_params(axis = 'y') #,labelcolor = color)
ax2.set_ylabel('Change in oil price WTI', size = 15)
ax3 = ax.twinx()
break_year = np.datetime64(date(2008, 1, 1).strftime('%Y-%m-%d'),'Y')
ax3.axvline(break_year, ls='--', color='r', alpha = 0.5)
ax3.axis('off')
ax4 = ax.twinx()
break_year = np.datetime64(date(2010, 1, 1).strftime('%Y-%m-%d'),'Y')
ax4.axvline(break_year, ls='--', color='r', alpha = 0.5)
ax4.axis('off')
# rotates and right aligns the x labels, and moves the bottom of the
# axes up to make room for them
fig.autofmt_xdate()
plt.show()
oil_price_modified = oil_price.copy()
year_53 = [1992,1998,2004,2009,2015,2020]
for i in year_53:
week = oil_price_modified[oil_price_modified['Year'] == 53]['Week']
oil_price_modified[oil_price_modified['Week'] == 53]['Week'] = 1
oil_price_modified[oil_price_modified['Week'] == 53]['Year'] = i+1
oil_price_modified[oil_price_modified['Year'] == 1992]
from pandas import read_csv
from pandas import DataFrame
from pandas import Grouper
from matplotlib import pyplot
groups = oil_price[['Year','oil_spot_WTI']].groupby(Grouper('Year'))
years = DataFrame()
att = []
att2 = []
plt.figure(figsize = (15,8))
for name, group in groups:
if len(group['oil_spot_WTI'].values) == 53:
years[name] = group['oil_spot_WTI'].values[:-1]
elif len(group['oil_spot_WTI'].values) < 52:
pass
else:
years[name] = group['oil_spot_WTI'].values
plt.style.use('seaborn-darkgrid')
years.boxplot( notch=True, patch_artist=True)
ax.grid(color='r', linestyle='-', linewidth=1)
plt.xticks(rotation = 45, fontsize = 12, weight = 'bold')
plt.yticks(fontsize = 12, weight = 'bold')
plt.xlabel("Year", fontsize = 15, weight = 'bold')
plt.ylabel("Oil spot WTI (USD/bbl)", fontsize = 15, weight = 'bold')
plt.title("Oil spot WTI (USD/bbl) since 1992", fontsize = 20, weight = 'bold', x = 0.5, y = 1.05)
plt.show()
from pandas import read_csv
from pandas import DataFrame
from pandas import Grouper
from matplotlib import pyplot
temp_frame = oil_price[['Year','oil_spot_WTI']]
k = np.log(temp_frame[['oil_spot_WTI']]) - np.log(temp_frame[['oil_spot_WTI']].shift(1))
temp_frame['chg_oil_spot_WTI'] = k
temp_frame.dropna(inplace = True)
groups = temp_frame[['Year','chg_oil_spot_WTI']].groupby(Grouper('Year'))
years = DataFrame()
att = []
att2 = []
plt.figure(figsize = (15,8))
for name, group in groups:
if len(group['chg_oil_spot_WTI'].values) == 53:
years[name] = group['chg_oil_spot_WTI'].values[:-1]
elif len(group['chg_oil_spot_WTI'].values) < 52:
pass
else:
years[name] = group['chg_oil_spot_WTI'].values
plt.style.use('seaborn-darkgrid')
years.boxplot(notch=True, patch_artist=True)
plt.xticks(rotation = 45, fontsize = 12, weight = 'bold')
plt.yticks( fontsize = 12, weight = 'bold')
plt.xlabel("Year", fontsize = 15, weight = 'bold')
plt.ylabel("Oil Spot WTI Return", fontsize = 15, weight = 'bold')
plt.ylim((-0.4,0.4))
plt.title("Return in Oil spot WTI (USD/bbl) since 1992", fontsize = 20, weight = 'bold', x = 0.5, y = 1.05)
plt.show()
#GDP
gdp_us = pd.read_csv('GDP_US.csv')
gdp_china = pd.read_csv('GDP_China.csv')
gdp_europe = pd.read_csv('GDP_Europe.csv')
#Modifying GDP column names and set 'Date' as an index
gdp_yuan = gdp_china['CHNGDPNQDSMEI']
gdp_china = gdp_china.drop(columns = ('CHNGDPNQDSMEI'))
gdp_china['Chinese_yuan'] = gdp_yuan
gdp_euro = gdp_europe['CPMNACSCAB1GQEU28']
gdp_europe = gdp_europe.drop(columns = ('CPMNACSCAB1GQEU28'))
gdp_europe['Euro'] = gdp_euro
gdp_europe = gdp_europe.set_index('DATE')
gdp_us = gdp_us.set_index('DATE')
gdp_china = gdp_china.set_index('DATE')
#Create GDP table for analysis -- setting Par_Month as an index
import datetime
gdp_df = gdp_us.join(gdp_china).join(gdp_europe).dropna()
index_list = gdp_df.index.copy()
transform = []
for i in index_list:
x = datetime.datetime.strptime(i, '%Y-%m-%d').strftime('%Y%m')
transform.append(int(x))
gdp_df['gdp_date'] = gdp_df.index
gdp_df['Par_Month'] = transform
gdp_df = gdp_df.set_index('Par_Month')
gdp_df = gdp_df[['gdp_date','GDP','Chinese_yuan','Euro']].rename(columns = {"GDP" : "gdp_us", "Chinese_yuan" : "gdp_china",'Euro':'gdp_europe'})
gdp_df.head()
#Energy consumption
#Uploading Global temperature
glob_temp = pd.read_csv('Temperature_Anomalies.csv')
glob_temp = glob_temp.rename(columns = {'Year':'Par_Month'}).set_index('Par_Month')
co2_em = pd.read_csv('co2_emissions_by_country.csv')
co2_em_gas = pd.read_csv('co2_gaseous_emission_by_country.csv')
co2_em_liq = pd.read_csv('co2_liquid_emission_by_country.csv')
co2_em_sol = pd.read_csv('co2_solid_emission_by_country.csv')
def clean_em(data,region,ftype):
data = data[data['Country Name'] == region].drop(columns = ['Country Name','Country Code','Indicator Name','Indicator Code'])
index = data.index.values[0]
data = np.transpose(data).rename(columns = {index : 'World_Emission' + '_' + ftype })
data.index = data.index.astype(int)
return data
world_co2_em = clean_em(co2_em,'World','general').dropna()
world_co2_em_gas = clean_em(co2_em_gas,'World','gas').dropna()
world_co2_em_liq = clean_em(co2_em_liq,'World','liq').dropna()
world_co2_em_sol = clean_em(co2_em_sol,'World','sol').dropna()
#CO2Concentration
#Fixing -999/99 values in Carbon Dioxide concentration
co2_conc = pd.read_csv('co2_weekly_mlo.csv')
fixing_index = []
fixing_value = []
for i in range(len(co2_conc)):
if co2_conc.iloc[i].average == -999.99:
y = co2_conc.iloc[i].year
a = co2_conc[(co2_conc.year == y) & (co2_conc.average != -999.99)]['average'].values.mean()
fixing_index.append(i)
fixing_value.append(a)
else:
fixing_value.append(co2_conc.iloc[i].average)
co2_conc['average'] = fixing_value
import datetime
#Add Par week to dataframe
def weeknumber(year,month,date):
x = datetime.date(year,month,date).strftime("%V")
return x
parweek = []
for i in range(len(co2_conc)):
if int(co2_conc.iloc[i].month) < 10:
month_dum = '0' + str(int(co2_conc.iloc[i].month))
else:
month_dum = str(int(co2_conc.iloc[i].month))
#####################################
if int(co2_conc.iloc[i].day) < 10:
day_dum = '0' + str(int(co2_conc.iloc[i].month))
else:
day_dum = str(int(co2_conc.iloc[i].day))
#####################################
year = int(co2_conc.iloc[i].year)
year_dum = str(year)
month = int(month_dum)
day = int(day_dum)
weeknum = weeknumber(year,month,day)
#####################################
if int(weeknum) < 10:
weeknum_dum = ('0' + str(int(weeknum)))
else:
weeknum_dum = weeknum
#####################################
if month_dum == '01' and weeknum_dum == '53' :
year_dum = int(year - 1)
month_dum = 12
else:
pass
Par_week = str(year_dum) + str(month_dum) + str(weeknum_dum)
parweek.append(Par_week)
#parweek
co2_conc['Par_week'] = parweek
co2_conc['Par_week'] = co2_conc['Par_week'].astype(int)
co2_conc_edited = co2_conc.groupby('Par_week').last()
#co2_conc_edited = co2_conc.set_index('Par_week').iloc[:,:5].iloc[:,4:5]
oil_price.set_index('Par_Week', inplace = True)
plot_seasonal_month = co2_conc.query('year >= 2005 and year < 2020 and year != 2008').groupby(['year','month']).mean()[['average']].reset_index()
plot_seasonal_month_2008_2020 = co2_conc.query('year == 2008 or year == 2020').groupby(['year','month']).mean()[['average']].reset_index()
fig, ax = plt.subplots(figsize = (4,4))
plt.style.use('seaborn-darkgrid')
#plt.style.use('ggplot')
ax1 = plot_seasonal_month.groupby('year').plot(x = 'month',y = 'average', marker = '', color = 'grey', ax = ax)
ax2 = plot_seasonal_month_2008_2020.groupby('year').plot(x = 'month',y = 'average', marker = '', color = 'red', ax = ax)
legend = plot_seasonal_month.groupby('year').mean().index
ax.legend('') #legend, loc='upper right')
plt.xlim(1,14)
plt.xticks(rotation = 45, fontsize = 10, weight = 'bold')
plt.yticks( fontsize = 10, weight = 'bold')
plt.xlabel("Month", fontsize = 10, weight = 'bold')
plt.ylabel("Carbon Dioxide Conecentration (ppm)", fontsize = 10, weight = 'bold')
plt.title("Seasonal Carbon Dioxide concentration plot", fontsize = 12, weight = 'bold', x = 0.5, y = 1.05)
#note for annotation
#for col in plot_seasonal_month.groupby('year').mean().index:
# plt.annotate(col,xy=(plt.xticks()[0][-1]+0.7, plot_seasonal_month.groupby('year'))) #df_plot[col].iloc[-1]))
#Create Oil price to CO2 concentration tables by joining data
oil_co2_df = oil_price.join(co2_conc_edited)
oil_co2_df.head()
#check duplicate
oil_co2_df[oil_co2_df.Weekindex.duplicated()]
#Using Augtolag = AIC
from statsmodels.tsa.stattools import adfuller, kpss
def adf_test(timeseries):
print ('Results of Augmented Dickey-Fuller Test:')
dftest = adfuller(timeseries, autolag='AIC')
dfoutput = pd.Series(dftest[0:4], index=['Test Statistic','p-value','#Lags Used','Number of Observations Used'])
for key,value in dftest[4].items():
dfoutput['Critical Value (%s)'%key] = value
print (dfoutput)
def kpss_test(timeseries):
print ('Results of KPSS Test:')
kpsstest = kpss(timeseries) #, regression='ct')
kpss_output = pd.Series(kpsstest[0:3], index=['Test Statistic','p-value','Lags Used'])
for key,value in kpsstest[3].items():
kpss_output['Critical Value (%s)'%key] = value
print (kpss_output)
#oil_co2_df['chg_gasoline_price'] = np.log(oil_co2_df.chg_Gasoline_price_US.shift(0)) - np.log(oil_co2_df. chg_Gasoline_price_US.shift(1))
co2_conc_edited['chg_co2_conc'] = np.log(co2_conc_edited.average.shift(0)) - np.log(co2_conc_edited.average.shift(1))
co2_conc_edited = co2_conc_edited.dropna()
# Par week
import statsmodels.api as sm
#from statsmodels.tsa.arima.model import ARIMA
fig = plt.figure(figsize=(12,8))
fig.suptitle('Weekly timeframe', size = 20)
ax1 = fig.add_subplot(221)
fig = sm.graphics.tsa.plot_acf(oil_price.chg_Gasoline_price_US, lags=10, ax=ax1)
ax1.set_title('Weekly gasoline_ACF')
ax2 = fig.add_subplot(222)
fig = sm.graphics.tsa.plot_pacf(oil_price.chg_Gasoline_price_US, lags=10, ax=ax2)
ax2.set_title('Weekly gasoline_PACF')
fig = plt.figure(figsize=(12,8))
ax3 = fig.add_subplot(223)
fig = sm.graphics.tsa.plot_acf(co2_conc_edited['chg_co2_conc'], lags=10, ax=ax3)
ax3.set_title('Weekly CO2_ACF')
ax4 = fig.add_subplot(224)
fig = sm.graphics.tsa.plot_pacf(co2_conc_edited['chg_co2_conc'], lags=10, ax=ax4)
ax4.set_title('Weekly CO2_PACF')
#In papaer
# Par week
import statsmodels.api as sm
#from statsmodels.tsa.arima.model import ARIMA
fig = plt.figure(figsize=(12,8))
fig.suptitle('Weekly timeframe', size = 20)
ax1 = fig.add_subplot(221)
fig = sm.graphics.tsa.plot_acf(oil_price.chg_Gasoline_price_US, lags=10, ax=ax1)
ax1.set_title('Weekly gasoline_ACF')
fig = plt.figure(figsize=(12,8))
fig.suptitle('Weekly timeframe', size = 20)
ax1 = fig.add_subplot(222)
fig = sm.graphics.tsa.plot_acf(np.power(oil_price.chg_Gasoline_price_US,2), lags=10, ax=ax1)
ax1.set_title('Weekly gasoline ACF Squared')
fig = plt.figure(figsize=(12,8))
ax3 = fig.add_subplot(223)
fig = sm.graphics.tsa.plot_acf(co2_conc_edited['chg_co2_conc'], lags=10, ax=ax3)
ax3.set_title('Weekly CO2_ACF')
fig = plt.figure(figsize=(12,8))
ax3 = fig.add_subplot(224)
fig = sm.graphics.tsa.plot_acf(np.power(co2_conc_edited['chg_co2_conc'],2), lags=10, ax=ax3)
ax3.set_title('Weekly CO2 ACF Squared')
fig = plt.figure(figsize=(8,8))
ax1 = fig.add_subplot(221)
fig = sm.graphics.tsa.plot_pacf(oil_price.chg_Gasoline_price_US, lags=10, ax=ax1)
ax1.set_title('Weekly gasoline PACF', fontsize = 12, weight = 'bold')
ax1 = fig.add_subplot(222)
fig = sm.graphics.tsa.plot_pacf(np.power(oil_price.chg_Gasoline_price_US,2), lags=10, ax=ax1)
ax1.set_title('Weekly gasoline PACF Squared', fontsize = 12, weight = 'bold')
ax3 = fig.add_subplot(223)
fig = sm.graphics.tsa.plot_pacf(co2_conc_edited['chg_co2_conc'], lags=10, ax=ax3)
ax3.set_title('Weekly CO2 PACF', fontsize = 12, weight = 'bold')
ax3 = fig.add_subplot(224)
fig = sm.graphics.tsa.plot_pacf(np.power(co2_conc_edited['chg_co2_conc'],2), lags=10, ax=ax3)
ax3.set_title('Weekly CO2 PACF Squared', fontsize = 12, weight = 'bold')
print('Weekly CO2')
print(adf_test(co2_conc_edited['average']))
#print(' ')
#print(kpss_test(co2_conc_edited['average']))
print('Weekly Gasoline Price')
print(adf_test(oil_price.Gasoline_Price_US))
print('Weekly First diff gasoline price')
print(adf_test(oil_price.chg_Gasoline_price_US))
Par_month = []
for i in range(len(co2_conc)):
if int(co2_conc.loc[i].month) < 10:
Par_month.append(str(int(co2_conc.loc[i].year)) + str(0) + str(int(co2_conc.loc[i].month)))
else:
Par_month.append(str(int(co2_conc.loc[i].year)) + str(int(co2_conc.loc[i].month)))
co2_conc['Par_month'] = Par_month
co2_conc_month = co2_conc.groupby('Par_month').last()
co2_conc_month = (np.log(co2_conc_month.average.shift(0)) - np.log(co2_conc_month.average.shift(1)))
co2_conc_month = co2_conc_month.dropna()
#Par Month
oil_co2_month = oil_price.groupby('Par_Month').last()
gasoline_month = np.log(oil_co2_month.Gasoline_Price_US.shift(0)) - np.log(oil_co2_month.Gasoline_Price_US.shift(1))
gasoline_month = gasoline_month.dropna()
co2_conc_month
# Par week
import statsmodels.api as sm
#from statsmodels.tsa.arima.model import ARIMA
fig = plt.figure(figsize=(12,8))
fig.suptitle('Monthly timeframe', size = 20)
ax1 = fig.add_subplot(221)
fig = sm.graphics.tsa.plot_acf(gasoline_month, lags=24, ax=ax1)
ax2 = fig.add_subplot(222)
fig = sm.graphics.tsa.plot_pacf(gasoline_month, lags=24, ax=ax2)
fig = plt.figure(figsize=(12,8))
ax3 = fig.add_subplot(223)
fig = sm.graphics.tsa.plot_acf(co2_conc_month, lags=24, ax=ax3)
ax4 = fig.add_subplot(224)
fig = sm.graphics.tsa.plot_pacf(co2_conc_month, lags=24, ax=ax4)
print('Monthly CO2')
print(adf_test(co2_conc.groupby('Par_month').last()['average']))
fig = plt.figure(figsize=(12,8))
fig.suptitle('Monthly CO2', size = 20)
data = co2_conc.groupby('Par_month').last()['average']
ax1 = fig.add_subplot(221)
fig = sm.graphics.tsa.plot_acf(data, lags=24, ax=ax1)
ax2 = fig.add_subplot(222)
fig = sm.graphics.tsa.plot_pacf(data, lags=24, ax=ax2)
print('Monthly First diff CO2')
print(adf_test(co2_conc_month))
fig = plt.figure(figsize=(12,8))
fig.suptitle('Monthly First diff CO2', size = 20)
data = co2_conc_month
ax1 = fig.add_subplot(221)
fig = sm.graphics.tsa.plot_acf(data, lags=24, ax=ax1)
ax2 = fig.add_subplot(222)
fig = sm.graphics.tsa.plot_pacf(data, lags=24, ax=ax2)
hia_month = (co2_conc_month - co2_conc_month.shift(12))
hia_month.dropna(inplace = True)
print('Monthly first diff CO2')
print(adf_test(hia_month))
fig = plt.figure(figsize=(12,8))
fig.suptitle('Monthly First diff Seasonal CO2', size = 20)
data = hia_month
ax1 = fig.add_subplot(221)
fig = sm.graphics.tsa.plot_acf(data, lags=10, ax=ax1)
ax2 = fig.add_subplot(222)
fig = sm.graphics.tsa.plot_pacf(data, lags=10, ax=ax2)
print('Monthly gasoline')
print(adf_test(oil_co2_df.groupby('Par_Month').last().Gasoline_Price_US))
hia_gas_month = (gasoline_month - gasoline_month.shift(12))
hia_gas_month.dropna(inplace = True)
print('Monthly first diff seasonal gasoline')
print(adf_test(hia_gas_month))
print('Monthly first diff gasoline')
print(adf_test(gasoline_month))
Par_quarter = []
error = []
for i in range(len(co2_conc)):
if (int(co2_conc.loc[i].month) >= 10 and int(co2_conc.loc[i].month) <= 12):
Par_quarter.append(str(int(co2_conc.loc[i].year)) + str(0) + str(4))
elif (int(co2_conc.loc[i].month) > 0 and int(co2_conc.loc[i].month) <= 3):
Par_quarter.append(str(int(co2_conc.loc[i].year)) + str(0) + str(1))
elif (int(co2_conc.loc[i].month) > 3 and int(co2_conc.loc[i].month) <= 6):
Par_quarter.append(str(int(co2_conc.loc[i].year)) + str(0) + str(2))
elif (int(co2_conc.loc[i].month) > 6 & int(co2_conc.loc[i].month) <= 9):
Par_quarter.append(str(int(co2_conc.loc[i].year)) + str(0) + str(3))
else:
#print(co2_conc.loc[i])
#error.append(co2_conc.loc[i])
Par_quarter.append(str(int(co2_conc.loc[i].year)) + str(0) + str(999))
Par_quarter
co2_conc['Par_quarter'] = Par_quarter
co2_conc_quarter = co2_conc.groupby('Par_quarter').last()
#co2_conc_quarter
co2_conc_quarter = (np.log(co2_conc_quarter.average.shift(0)) - np.log(co2_conc_quarter.average.shift(1)))
co2_conc_quarter = co2_conc_quarter.dropna()
#Par Quarter
oil_co2_quarter_df = oil_price.query('Month in [12,3,6,9]') #.groupby(['Year','Month']).chg_Gasoline_price_US.last()
gas_quarter = oil_co2_quarter_df.groupby('Par_Month').Gasoline_Price_US.last()
gas_quarter = (np.log(gas_quarter.shift(0)) - np.log(gas_quarter.shift(1)))
gas_quarter = gas_quarter.dropna()
# Par week
import statsmodels.api as sm
#from statsmodels.tsa.arima.model import ARIMA
fig = plt.figure(figsize=(12,8))
fig.suptitle('Quarterly timeframe', size = 20)
ax1 = fig.add_subplot(221)
fig = sm.graphics.tsa.plot_acf(gas_quarter, lags=10, ax=ax1)
ax2 = fig.add_subplot(222)
fig = sm.graphics.tsa.plot_pacf(gas_quarter, lags=10, ax=ax2)
fig = plt.figure(figsize=(12,8))
ax3 = fig.add_subplot(223)
fig = sm.graphics.tsa.plot_acf(co2_conc_quarter, lags=10, ax=ax3)
ax4 = fig.add_subplot(224)
fig = sm.graphics.tsa.plot_pacf(co2_conc_quarter, lags=10, ax=ax4)
print('Quarterly CO2')
print(adf_test(co2_conc.groupby('Par_quarter').last().average))
fig = plt.figure(figsize=(12,8))
fig.suptitle('Quarterly CO2', size = 20)
data = co2_conc.groupby('Par_quarter').last().average
ax1 = fig.add_subplot(221)
fig = sm.graphics.tsa.plot_acf(data, lags=10, ax=ax1)
ax2 = fig.add_subplot(222)
fig = sm.graphics.tsa.plot_pacf(data, lags=10, ax=ax2)
print('Quarterly First diff CO2')
print(adf_test(co2_conc_quarter))
fig = plt.figure(figsize=(12,8))
fig.suptitle('Quarterly First diff CO2', size = 20)
data = co2_conc_quarter
ax1 = fig.add_subplot(221)
fig = sm.graphics.tsa.plot_acf(data, lags=8, ax=ax1)
ax2 = fig.add_subplot(222)
fig = sm.graphics.tsa.plot_pacf(data, lags=8, ax=ax2)
print('Quarterly First diff seasonal CO2')
hia_quarter = (co2_conc_quarter - co2_conc_quarter.shift(4))
hia_quarter.dropna(inplace = True)
print(adf_test(hia_quarter))
fig = plt.figure(figsize=(12,8))
fig.suptitle('Quarterly First diff Seasonal CO2', size = 20)
data = hia_quarter
ax1 = fig.add_subplot(221)
fig = sm.graphics.tsa.plot_acf(data, lags=8, ax=ax1)
ax2 = fig.add_subplot(222)
fig = sm.graphics.tsa.plot_pacf(data, lags=8, ax=ax2)
fig = plt.figure(figsize=(15,8))
#fig.suptitle('Monthly First diff CO2', size = 20)
#data = co2_conc_month
ax1 = fig.add_subplot(111)
fig1 = sm.graphics.tsa.plot_acf(co2_conc_month, lags=24, ax=ax1, alpha=.01)
#ax1 = fig.add_subplot(121)
fig1 = sm.graphics.tsa.plot_acf(hia_month, lags=24, ax=ax1, alpha=.01)
ax1.set_title('Autocorrelation - Monthly First Diff CO2', fontsize = 15)
#ax1.legend((l2, l4), ('oscillatory', 'damped'), loc='upper right', shadow=True)
fig2 = plt.figure(figsize=(15,8))
ax1 = fig2.add_subplot(111)
fig2 = sm.graphics.tsa.plot_acf(co2_conc_quarter, lags=24, ax=ax1, alpha=.01)
#ax1 = fig.add_subplot(121)
fig = sm.graphics.tsa.plot_acf(hia_quarter, lags=24, ax=ax1, alpha=.01)
ax1.set_title('Autocorrelation - Quarterly First Diff CO2', fontsize = 15)
plt.style.use('seaborn-darkgrid')
fig = plt.figure(figsize=(4,4))
fig.suptitle('Autocorrelation of Carbon Dioxide \n concentration', size = 12, x= 0.5, y= 1.05, weight = 'bold')
ax1 = fig.add_subplot(221)
fig = sm.graphics.tsa.plot_acf(co2_conc_month, lags=12, ax=ax1)
ax1.set_title('First Diff CO2', fontsize = 10, weight = 'bold')
ax1.set_ylabel('Monthly', fontsize = 10, weight = 'bold')
ax2 = fig.add_subplot(222)
fig = sm.graphics.tsa.plot_acf(hia_month, lags=12, ax=ax2)
ax2.set_title('First Diff Seasonal CO2', fontsize = 10, weight = 'bold')
#fig = plt.figure(figsize=(4,4))
ax3 = fig.add_subplot(223)
fig = sm.graphics.tsa.plot_acf(co2_conc_quarter, lags=12, ax=ax3)
ax3.set_title('') #'First Diff CO2', fontsize = 10)
ax3.set_ylabel('Quarterly', fontsize = 10, weight = 'bold')
ax4 = fig.add_subplot(224)
fig = sm.graphics.tsa.plot_acf(hia_quarter, lags=12, ax=ax4)
ax4.set_title('', fontsize = 10, weight = 'bold')
ax4.set_title('') #'First Diff Seasonal CO2', fontsize = 10)
print('Quarterly Gasoline price')
print(adf_test(oil_co2_quarter_df.groupby('Par_Month').Gasoline_Price_US.last()))
print('Quarterly first diff Gasoline price')
print(adf_test(gas_quarter))
hia_gas_quarter = (gas_quarter - gas_quarter.shift(4))
hia_gas_quarter.dropna(inplace = True)
print('Quarterly first diff seasonal gasoline')
print(adf_test(hia_gas_quarter))
#co2_conc_quarter
Par_quarter = []
error = []
for i in range(len(co2_conc)):
if (int(co2_conc.loc[i].month) >= 10 and int(co2_conc.loc[i].month) <= 12):
Par_quarter.append(str(int(co2_conc.loc[i].year)) + str(0) + str(4))
elif (int(co2_conc.loc[i].month) > 0 and int(co2_conc.loc[i].month) <= 3):
Par_quarter.append(str(int(co2_conc.loc[i].year)) + str(0) + str(1))
elif (int(co2_conc.loc[i].month) > 3 and int(co2_conc.loc[i].month) <= 6):
Par_quarter.append(str(int(co2_conc.loc[i].year)) + str(0) + str(2))
elif (int(co2_conc.loc[i].month) >= 6 & int(co2_conc.loc[i].month) <= 9):
Par_quarter.append(str(int(co2_conc.loc[i].year)) + str(0) + str(3))
else:
#print(co2_conc.loc[i])
#error.append(co2_conc.loc[i])
Par_quarter.append(str(int(co2_conc.loc[i].year)) + str(0) + str(999))
Par_quarter
co2_conc['Par_quarter'] = Par_quarter
co2_conc_quarter = co2_conc.groupby('Par_quarter').last()
co2_conc_quarter_no_dif = co2_conc_quarter.average #(np.log(co2_conc_quarter.average.shift(0)) - np.log(co2_conc_quarter.average.shift(1)))
co2_conc_quarter_second_df = (np.log(co2_conc_quarter.average.shift(0)) - np.log(co2_conc_quarter.average.shift(1)))
co2_conc_quarter_second_df = co2_conc_quarter_second_df.shift(0) - co2_conc_quarter_second_df.shift(1)
co2_conc_quarter_second_df = co2_conc_quarter_second_df.dropna()
print('Quarterly second diff seasonal CO2')
print(adf_test(co2_conc_quarter_no_dif))
print('Quarterly second diff seasonal CO2')
print(adf_test(co2_conc_quarter_second_df))
sm.graphics.tsa.plot_pacf(co2_conc_quarter_second_df, lags=10, ax=ax2)
#Par year
oil_co2_year_df = oil_price.query('Month in [12]') #.groupby(['Year','Month']).chg_Gasoline_price_US.last()
gas_year = oil_co2_year_df.groupby('Year').Gasoline_Price_US.last()
gas_year = (np.log(gas_year.shift(0)) - np.log(gas_year.shift(1)))
gas_year = gas_year.dropna()
co2_conc_year = co2_conc.groupby('year').last()
#co2_conc_quarter
co2_conc_year = (np.log(co2_conc_year.average.shift(0)) - np.log(co2_conc_year.average.shift(1)))
co2_conc_year = co2_conc_year.dropna()
# Par week
import statsmodels.api as sm
#from statsmodels.tsa.arima.model import ARIMA
fig = plt.figure(figsize=(12,8))
fig.suptitle('Yearly timeframe', size = 20)
ax1 = fig.add_subplot(221)
fig = sm.graphics.tsa.plot_acf(gas_year, lags=10, ax=ax1)
ax2 = fig.add_subplot(222)
fig = sm.graphics.tsa.plot_pacf(gas_year, lags=10, ax=ax2)
fig = plt.figure(figsize=(12,8))
ax3 = fig.add_subplot(223)
fig = sm.graphics.tsa.plot_acf(co2_conc_year, lags=10, ax=ax3)
ax4 = fig.add_subplot(224)
fig = sm.graphics.tsa.plot_pacf(co2_conc_year, lags=10, ax=ax4)
print('Yearly CO2')
print(adf_test(co2_conc.groupby('year').last().average))
print('Yearly first diff CO2')
print(adf_test(co2_conc_year))
print('Yearly Gasoline price')
print(adf_test(oil_co2_year_df.groupby('Year').Gasoline_Price_US.last()))
print('Yearly first diff Gasoline price')
print(adf_test(gas_year))
#Using Augtolag = AIC
from statsmodels.tsa.stattools import adfuller, kpss
def adf_test(timeseries):
print ('Results of Augmented Dickey-Fuller Test:')
dftest = adfuller(timeseries, autolag='AIC')
dfoutput = pd.Series(dftest[0:4], index=['Test Statistic','p-value','#Lags Used','Number of Observations Used'])
for key,value in dftest[4].items():
dfoutput['Critical Value (%s)'%key] = value
print (dfoutput)
def kpss_test(timeseries):
print ('Results of KPSS Test:')
kpsstest = kpss(timeseries) #, regression='ct')
kpss_output = pd.Series(kpsstest[0:3], index=['Test Statistic','p-value','Lags Used'])
for key,value in kpsstest[3].items():
kpss_output['Critical Value (%s)'%key] = value
print (kpss_output)
print(adf_test(oil_price['oil_spot_WTI']))
print(' ')
print(kpss_test(oil_price['oil_spot_WTI']))
#Crude oil WTI_weekly
first_dif_conc = (np.log(oil_price['oil_spot_WTI'].shift(0)) - np.log(oil_price['oil_spot_WTI'].shift(1))).dropna()
print(adf_test(first_dif_conc))
print(' ')
print(kpss_test(first_dif_conc))
#Crude oil WTI Month
oil_co2_month = oil_price.groupby('Par_Month')['oil_spot_WTI'].last()
print("Crude oil WTI Month")
print(adf_test(oil_co2_month))
oil_month = np.log(oil_co2_month.shift(0)) - np.log(oil_co2_month.shift(1))
oil_month = oil_month.dropna()
print("First Diff Crude oil WTI Month")
print(adf_test(oil_month))
oil_month = oil_month - oil_month.shift(12)
oil_month = oil_month.dropna()
print("First Diff Seasonal Crude oil WTI Month")
print(adf_test(oil_month))
#Crude oil WTI Quarter
function = oil_price.query('Month in [12,3,6,9]') #.groupby(['Year','Month']).chg_Gasoline_price_US.last()
function = function.groupby('Par_Month')['oil_spot_WTI'].last()
print("Crude oil WTI Quarter")
print(adf_test(function))
function_2 = np.log(function.shift(0)) - np.log(function.shift(1))
function_2 = function_2.dropna()
print("First Diff Crude oil WTI Quarter")
print(adf_test(function_2))
function_3 = function_2 - function_2.shift(4)
function_3 = function_3.dropna()
print("First Diff Seasonal Crude oil WTI Quarter")
print(adf_test(function_3))
#Crude oil WTI Month
#Change this line and title
index = 'oil_spot_Brent'
oil_co2_month = oil_price.groupby('Par_Month')[index].last()
print("Crude oil Brent Month")
print(adf_test(oil_co2_month))
oil_month = np.log(oil_co2_month.shift(0)) - np.log(oil_co2_month.shift(1))
oil_month = oil_month.dropna()
print("First Diff Crude oil Brent Month")
print(adf_test(oil_month))
oil_month = oil_month - oil_month.shift(12)
oil_month = oil_month.dropna()
print("First Diff Seasonal Crude oil WTI Month")
print(adf_test(oil_month))
#Crude oil WTI Quarter
function = oil_price.query('Month in [12,3,6,9]') #.groupby(['Year','Month']).chg_Gasoline_price_US.last()
function = function.groupby('Par_Month')[index].last()
print("Crude oil Brent Quarter")
print(adf_test(function))
function_2 = np.log(function.shift(0)) - np.log(function.shift(1))
function_2 = function_2.dropna()
print("First Diff Crude oil Brent Quarter")
print(adf_test(function_2))
function_3 = function_2 - function_2.shift(4)
function_3 = function_3.dropna()
print("First Diff Seasonal Crude oil Brent Quarter")
print(adf_test(function_3))
print(adf_test(oil_price['future_oil_contract_1']))
first_dif_conc = (np.log(oil_price['future_oil_contract_1'].shift(0)) - np.log(oil_price['future_oil_contract_1'].shift(1))).dropna()
print(adf_test(first_dif_conc))
#Crude oil WTI Month
#Change this line and title
index = 'future_oil_contract_1'
oil_co2_month = oil_price.groupby('Par_Month')[index].last()
print(index +" Month")
print(adf_test(oil_co2_month))
oil_month = np.log(oil_co2_month.shift(0)) - np.log(oil_co2_month.shift(1))
oil_month = oil_month.dropna()
print("First Diff " + index + " Month")
print(adf_test(oil_month))
oil_month = oil_month - oil_month.shift(12)
oil_month = oil_month.dropna()
print("First Diff Seasonal "+index+" Month")
print(adf_test(oil_month))
#Crude oil WTI Quarter
function = oil_price.query('Month in [12,3,6,9]') #.groupby(['Year','Month']).chg_Gasoline_price_US.last()
function = function.groupby('Par_Month')[index].last()
print(index + " Quarter")
print(adf_test(function))
function_2 = np.log(function.shift(0)) - np.log(function.shift(1))
function_2 = function_2.dropna()
print("First Diff "+index+" Quarter")
print(adf_test(function_2))
function_3 = function_2 - function_2.shift(4)
function_3 = function_3.dropna()
print("First Diff Seasonal "+index +" Quarter")
print(adf_test(function_3))
print(adf_test(oil_price['sugar_price_us']))
#Sugar price week
first_dif_conc = (np.log(oil_price['sugar_price_us'].shift(0)) - np.log(oil_price['sugar_price_us'].shift(1))).dropna()
print(adf_test(first_dif_conc))
#Crude oil WTI Month
#Change this line and title
index = 'sugar_price_us'
oil_co2_month = oil_price.groupby('Par_Month')[index].last()
print(index +" Month")
print(adf_test(oil_co2_month))
oil_month = np.log(oil_co2_month.shift(0)) - np.log(oil_co2_month.shift(1))
oil_month = oil_month.dropna()
print("First Diff " + index + " Month")
print(adf_test(oil_month))
oil_month = oil_month - oil_month.shift(12)
oil_month = oil_month.dropna()
print("First Diff "+index+" Month")
print(adf_test(oil_month))
#Crude oil WTI Quarter
function = oil_price.query('Month in [12,3,6,9]') #.groupby(['Year','Month']).chg_Gasoline_price_US.last()
function = function.groupby('Par_Month')[index].last()
print(index + " Quarter")
print(adf_test(function))
function_2 = np.log(function.shift(0)) - np.log(function.shift(1))
function_2 = function_2.dropna()
print("First Diff "+index+" Quarter")
print(adf_test(function_2))
function_3 = function_2 - function_2.shift(4)
function_3 = function_3.dropna()
print("First Diff Seasonal "+index +" Quarter")
print(adf_test(function_3))
#Sugar price month
oil_co2_month = oil_co2_df.groupby('Par_Month').last()
sugar_month = np.log(oil_co2_month.sugar_price_us.shift(0)) - np.log(oil_co2_month.sugar_price_us.shift(1))
gasoline_month = gasoline_month.dropna()
print(adf_test(oil_price['ethanol_price_us'].dropna()))
first_dif_conc = (np.log(oil_price['ethanol_price_us'].dropna().shift(0)) - np.log(oil_price['ethanol_price_us'].dropna().shift(1))).dropna()
print(adf_test(first_dif_conc))
#Crude oil WTI Month
#Change this line and title
index = 'ethanol_price_us'
oil_co2_month = oil_price.groupby('Par_Month')[index].last()
oil_co2_month.dropna(inplace = True)
print(index +" Month")
print(adf_test(oil_co2_month))
oil_month = np.log(oil_co2_month.shift(0)) - np.log(oil_co2_month.shift(1))
oil_month = oil_month.dropna()
print("First Diff " + index + " Month")
print(adf_test(oil_month))
oil_month = oil_month - oil_month.shift(12)
oil_month = oil_month.dropna()
print("First Diff "+index+" Month")
print(adf_test(oil_month))
#Crude oil WTI Quarter
function = oil_price.query('Month in [12,3,6,9]') #.groupby(['Year','Month']).chg_Gasoline_price_US.last()
function = function.groupby('Par_Month')[index].last()
function.dropna(inplace = True)
print(index + " Quarter")
print(adf_test(function))
function_2 = np.log(function.shift(0)) - np.log(function.shift(1))
function_2 = function_2.dropna()
print("First Diff "+index+" Quarter")
print(adf_test(function_2))
function_3 = function_2 - function_2.shift(4)
function_3 = function_3.dropna()
print("First Diff Seasonal "+index +" Quarter")
print(adf_test(function_3))
class color:
PURPLE = '\033[95m'
CYAN = '\033[96m'
DARKCYAN = '\033[36m'
BLUE = '\033[94m'
GREEN = '\033[92m'
YELLOW = '\033[93m'
RED = '\033[91m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
END = '\033[0m'
print(color.BOLD +'GDP_US' + color.END)
print(adf_test(gdp_df['gdp_us']))
print(' ')
print(kpss_test(gdp_df['gdp_us']))
print(color.BOLD +'GDP_China' + color.END)
print(adf_test(gdp_df['gdp_china']))
print(' ')
print(kpss_test(gdp_df['gdp_china']))
print(color.BOLD +'GDP_Europe' + color.END)
print(adf_test(gdp_df['gdp_europe']))
print(' ')
print(kpss_test(gdp_df['gdp_europe']))
first_dif_gdp = (np.log(gdp_df[['gdp_us','gdp_china','gdp_europe']].shift(0)) - np.log(gdp_df[['gdp_us','gdp_china','gdp_europe']].shift(1))).dropna()
print(color.BOLD +'GDP_US' + color.END)
print(adf_test(first_dif_gdp['gdp_us']))
print(' ')
print(kpss_test(first_dif_gdp['gdp_us']))
print(color.BOLD +'GDP_China' + color.END)
print(adf_test(first_dif_gdp['gdp_china']))
print(' ')
print(kpss_test(first_dif_gdp['gdp_china']))
print(color.BOLD +'GDP_Europe' + color.END)
print(adf_test(first_dif_gdp['gdp_europe']))
print(' ')
print(kpss_test(first_dif_gdp['gdp_europe']))
(first_dif_gdp.shift(0)) - first_dif_gdp.shift(4)
#First diff seasonal GDP
first_dif_seasonal_gdp = (first_dif_gdp.shift(0)) - first_dif_gdp.shift(4)
first_dif_seasonal_gdp.dropna(inplace = True)
print(color.BOLD +'GDP_US' + color.END)
print(adf_test(first_dif_seasonal_gdp['gdp_us']))
print(' ')
print(kpss_test(first_dif_seasonal_gdp['gdp_us']))
print(color.BOLD +'GDP_China' + color.END)
print(adf_test(first_dif_seasonal_gdp['gdp_china']))
print(' ')
print(kpss_test(first_dif_seasonal_gdp['gdp_china']))
print(color.BOLD +'GDP_Europe' + color.END)
print(adf_test(first_dif_seasonal_gdp['gdp_europe']))
print(' ')
print(kpss_test(first_dif_seasonal_gdp['gdp_europe']))
#GDP weekly
#oil_price.join
gdp_date_list = gdp_df.gdp_date.to_list()
year = []
month = []
quarter = []
weeknum = []
for i in range(len(gdp_date_list)):
if int(datetime.datetime.strptime(gdp_date_list[i],'%Y-%m-%d').strftime('%m')) == 1:
y = int(datetime.datetime.strptime(gdp_date_list[i],'%Y-%m-%d').strftime('%Y'))-1
m = int(12)
d = int(4)
year.append(y)
month.append(m)
quarter.append(d)
else:
y = int(datetime.datetime.strptime(gdp_date_list[i],'%Y-%m-%d').strftime('%Y'))
m = int(datetime.datetime.strptime(gdp_date_list[i],'%Y-%m-%d').strftime('%m'))-1
d = int(datetime.datetime.strptime(gdp_date_list[i],'%Y-%m-%d').strftime('%m'))//3
year.append(y)
month.append(m)
quarter.append(d)
gdp_df['Year'] = year
gdp_df['Month'] = month
gdp_df['Quarter'] = quarter
gdp_df.head()
oil_price_month = oil_price['Month'].to_list()
quarter = []
for i in range(len(oil_price_month)):
if oil_price_month[i] in [1,2,3]:
#print(oil_price_month[i])
quarter.append(1)
elif oil_price_month[i] in [4,5,6]:
quarter.append(2)
elif oil_price_month[i] in [7,8,9]:
quarter.append(3)
elif oil_price_month[i] in [10,11,12]:
quarter.append(4)
else:
quarter.append(oil_price_month[i])
oil_price['Quarter'] = quarter
test_stationary_gdp_weekly = pd.merge(oil_price, gdp_df, how="left", on=['Year','Quarter']).drop(columns = ['Month_y']).rename(columns = {'Month_x':'Month'}).set_index('Par_Month') #.to_csv('quick_look_gdp_week1.csv')
test_stationary_gdp_weekly = test_stationary_gdp_weekly.dropna(subset = ['gdp_date'])[['gdp_china','gdp_us','gdp_europe']]
print(color.BOLD +'GDP_US' + color.END)
print(adf_test(test_stationary_gdp_weekly['gdp_us']))
print(color.BOLD +'GDP_China' + color.END)
print(adf_test(test_stationary_gdp_weekly['gdp_china']))
print(color.BOLD +'GDP_Europe' + color.END)
print(adf_test(test_stationary_gdp_weekly['gdp_europe']))
first_dif_gdp_weekly = (np.log(test_stationary_gdp_weekly[['gdp_us','gdp_china','gdp_europe']].shift(0)) - np.log(test_stationary_gdp_weekly[['gdp_us','gdp_china','gdp_europe']].shift(1))).dropna()
print(color.BOLD +'GDP_US' + color.END)
print(adf_test(first_dif_gdp_weekly['gdp_us']))
print(color.BOLD +'GDP_China' + color.END)
print(adf_test(first_dif_gdp_weekly['gdp_china']))
print(color.BOLD +'GDP_Europe' + color.END)
print(adf_test(first_dif_gdp_weekly['gdp_europe']))
test_stationary_gdp_monthly = test_stationary_gdp_weekly.groupby('Par_Month').last()
print(color.BOLD +'GDP_US' + color.END)
print(adf_test(test_stationary_gdp_monthly['gdp_us']))
print(color.BOLD +'GDP_China' + color.END)
print(adf_test(test_stationary_gdp_monthly['gdp_china']))
print(color.BOLD +'GDP_Europe' + color.END)
print(adf_test(test_stationary_gdp_monthly['gdp_europe']))
first_dif_gdp_monthly = (np.log(test_stationary_gdp_monthly[['gdp_us','gdp_china','gdp_europe']].shift(0)) - np.log(test_stationary_gdp_monthly[['gdp_us','gdp_china','gdp_europe']].shift(1))).dropna()
first_dif_gdp_monthly_seasonal = first_dif_gdp_monthly.shift(0) - first_dif_gdp_monthly.shift(12)
first_dif_gdp_monthly_seasonal.dropna(inplace = True)
print(color.BOLD +'GDP_US' + color.END)
print(adf_test(first_dif_gdp_monthly['gdp_us']))
print(color.BOLD +'GDP_China' + color.END)
print(adf_test(first_dif_gdp_monthly['gdp_china']))
print(color.BOLD +'GDP_Europe' + color.END)
print(adf_test(first_dif_gdp_monthly['gdp_europe']))
print(color.BOLD +'GDP_US' + color.END)
print(adf_test(first_dif_gdp_monthly_seasonal['gdp_us']))
print(color.BOLD +'GDP_China' + color.END)
print(adf_test(first_dif_gdp_monthly_seasonal['gdp_china']))
print(color.BOLD +'GDP_Europe' + color.END)
print(adf_test(first_dif_gdp_monthly_seasonal['gdp_europe']))
print(adf_test(glob_temp['Celsius']))
print(' ')
print(kpss_test(glob_temp['Celsius']))
#Fix zero data
index_fix_data = glob_temp[glob_temp['Celsius'] == 0].index
for i in index_fix_data:
glob_temp.loc[i] = glob_temp.loc[i-1].copy()
first_dif_temp = (np.log(glob_temp['Celsius'].shift(0)) - np.log(glob_temp['Celsius'].shift(1))).dropna()
first_dif_temp = first_dif_temp.to_frame()
print(adf_test(first_dif_temp['Celsius']))
print(' ')
print(kpss_test(first_dif_temp['Celsius']))
#first_dif_temp.index
month_list = []
year_list = []
for i in glob_temp['Celsius'].index:
month_list.append(str(i)[-2:])
year_list.append(str(i)[:4])
glob_temp['Month'] = month_list
glob_temp['Year'] = year_list
#Global temp Month
#Change this line and title
index = 'Global temperature(Celsius)'
print(index +" Month")
print(adf_test(glob_temp['Celsius']))
glob_temp_month = np.log(glob_temp['Celsius'].shift(0)) - np.log(glob_temp['Celsius'].shift(1))
glob_temp_month = glob_temp_month.dropna()
print("First Diff " + index + " Month")
print(adf_test(glob_temp_month))
glob_temp_month = glob_temp_month - glob_temp_month.shift(12)
glob_temp_month = glob_temp_month.dropna()
print("First Diff seasonal"+index+" Month")
print(adf_test(glob_temp_month))
#Global temp Month
function = glob_temp.query('Month in ["12","03","06","09"]')['Celsius']
#function = function.groupby('Par_Month')[index].last()
function.dropna(inplace = True)
print(index + " Quarter")
print(adf_test(function))
function_2 = np.log(function.shift(0)) - np.log(function.shift(1))
function_2 = function_2.dropna()
print("First Diff "+index+" Quarter")
print(adf_test(function_2))
function_3 = function_2 - function_2.shift(4)
function_3 = function_3.dropna()
print("First Diff Seasonal "+index +" Quarter")
print(adf_test(function_3))
glob_temp.Month = glob_temp.Month.astype(int).to_list()
glob_temp.Year = glob_temp.Year.astype(int).to_list()
glob_temp_df = pd.merge(oil_price,glob_temp, how = 'left', on = ['Year','Month'])
glob_temp_df = glob_temp_df[['Celsius']].dropna()
#weekly test stationary
glob_temp_df_stationary = np.log(glob_temp_df) - np.log(glob_temp_df).shift(52)
glob_temp_df_stationary.dropna(inplace = True)
glob_temp_df_stationary
print(adf_test(glob_temp_df['Celsius']))
print(adf_test(glob_temp_df_stationary['Celsius']))
print(adf_test(world_co2_em['World_Emission_general']))
print(' ')
print(kpss_test(world_co2_em['World_Emission_general']))
first_dif_em = (np.log(world_co2_em['World_Emission_general'].shift(0)) - np.log(world_co2_em['World_Emission_general'].shift(1))).dropna()
first_dif_em = first_dif_em.to_frame()
print(adf_test(first_dif_em['World_Emission_general']))
print(' ')
print(kpss_test(first_dif_em['World_Emission_general']))
print(adf_test(co2_conc['average']))
print(' ')
print(kpss_test(co2_conc['average']))
first_dif_conc = (np.log(co2_conc.average.shift(0)) - np.log(co2_conc.average.shift(1))).dropna()
print(adf_test(first_dif_conc))
print(' ')
print(kpss_test(first_dif_conc))
# Choosing last Month data
quarter_oil_co2_df = oil_co2_df.groupby('Par_Month').last().copy()
glob_temp
gdp_df.drop(columns = ['Year','Month','Quarter'])
#Joining data to create big dataframe
q_analysis_df = quarter_oil_co2_df.join(gdp_df.drop(columns = ['Year','Month','Quarter'])).dropna(subset=['gdp_date','gdp_us','gdp_china','gdp_europe'])
q_analysis_df = q_analysis_df.join(glob_temp['Celsius'])
#Rename Carbon Dioxide concentration
temp = q_analysis_df['average']
q_analysis_df = q_analysis_df.drop(columns = 'average')
q_analysis_df['CO2_Concentration'] = temp
q_analysis_df = q_analysis_df.drop(columns = 'gdp_date')
q_analysis_df.head()
#Create first difference
temp_frame = (np.log(q_analysis_df[['gdp_china','gdp_us','gdp_europe','Celsius','CO2_Concentration']].shift(0)) - np.log(q_analysis_df[['gdp_china','gdp_us','gdp_europe','Celsius','CO2_Concentration']].shift(1)))
temp_frame = temp_frame.rename(columns = {'gdp_china' : 'chg_gdp_china','gdp_europe':'chg_gdp_europe','gdp_us':'chg_gdp_us','Celsius':'chg_celsius','CO2_Concentration':'chg_co2_concentration'})
q_analysis_df = q_analysis_df.merge(temp_frame, how='inner', on='Par_Month')
q_analysis_df.head()
q_analysis_df = q_analysis_df.iloc[1:]
q_analysis_df.columns
#Export data to output table
q_analysis_df.to_csv('quarterly_analysis_prep_data.csv')
q_analysis_df_correl = q_analysis_df[[ 'Gasoline_Price_US','CO2_Concentration','oil_spot_WTI', 'oil_spot_Brent', 'future_oil_contract_1','sugar_price_us', 'ethanol_price_us','gdp_us', 'gdp_china', 'gdp_europe', 'Celsius']]
q_analysis_df_correl.dropna(inplace = True)
q_analysis_df_correl_diff = np.log(q_analysis_df_correl) - np.log(q_analysis_df_correl.shift(1))
q_analysis_df_correl_diff.dropna(inplace = True)
from statsmodels.tsa.stattools import grangercausalitytests
maxlag=10
test = 'ssr-chi2test'
def grangers_causality_matrix(X_train, variables, test = 'ssr_chi2test', verbose=False):
dataset = pd.DataFrame(np.zeros((len(variables), len(variables))), columns=variables, index=variables)
for c in dataset.columns:
for r in dataset.index:
test_result = grangercausalitytests(X_train[[r,c]], maxlag=maxlag, verbose=False)
p_values = [round(test_result[i+1][0][test][1],4) for i in range(maxlag)]
if verbose: print(f'Y = {r}, X = {c}, P Values = {p_values}')
min_p_value = np.min(p_values)
dataset.loc[r,c] = min_p_value
dataset.columns = [var + '_x' for var in variables]
dataset.index = [var + '_y' for var in variables]
return dataset
granger_test = grangers_causality_matrix(q_analysis_df_correl_diff , variables = q_analysis_df_correl_diff.columns)
granger_test
import seaborn as sns
corr =round(q_analysis_df_correl_diff.corr(),2)
mask = np.zeros_like(corr, dtype=np.bool)
mask[np.triu_indices_from(mask)] = True
sns.heatmap(corr, xticklabels = corr.columns.values,yticklabels = corr.columns.values, annot = True,annot_kws = {'size':12}, cmap = 'vlag_r', mask = mask)
heat_map = plt.gcf()
heat_map.set_size_inches(15,8)
plt.xticks(fontsize = 15)
plt.yticks(fontsize = 15)
plt.show()
#Weekly Monthly timeframe
quick_anaysis = q_analysis_df.copy()
# test perfect number of lags used in model
import statsmodels.api as sm
nlag = 20
for i in range(nlag+1):
#print(i)
quick_anaysis_df_loop = quick_anaysis.copy()
#running = i.copy()
column_names = ['chg_oil_spot_WTI_Positive','chg_oil_spot_WTI_Negative','chg_gdp_us','chg_gdp_china','chg_gdp_europe']
print("Number of lag : ", str(i))
while i > 0:
text = 'lag' + '_' + str(i)
quick_anaysis_df_loop[text] = quick_anaysis['oil_spot_WTI'].shift(i)
quick_anaysis_df_loop['chg_oil_spot_WTI' + "_" + text] = quick_anaysis['oil_spot_WTI'].shift(i)
quick_anaysis_df_loop['chg_gdp_us' + "_" + text] = quick_anaysis['gdp_us'].shift(i)
quick_anaysis_df_loop['chg_gdp_china' + "_" + text] = quick_anaysis['gdp_china'].shift(i)
quick_anaysis_df_loop['chg_gdp_europe' + "_" + text] = quick_anaysis['gdp_europe'].shift(i)
column_names.append('chg_oil_spot_WTI' + "_" + text)
column_names.append('chg_gdp_us' + "_" + text)
column_names.append('chg_gdp_china' + "_" + text)
column_names.append('chg_gdp_europe' + "_" + text)
i = i - 1
#print(i+1)
#print(" ")
quick_anaysis_df_loop = quick_anaysis_df_loop.dropna()
x = quick_anaysis_df_loop[column_names]
reg_model = sm.OLS(quick_anaysis_df_loop['chg_co2_concentration'],x)
result = reg_model.fit()
print("R-squared : ", result.rsquared)
print("P-value : ")
print(round(result.pvalues,4))
print(" ")
%matplotlib inline
import numpy as np
from matplotlib import pyplot as plt
from statsmodels.tsa.vector_ar.vecm import *
import pandas
quick_analysis_ln = np.log(quick_anaysis[['oil_spot_WTI','gdp_us', 'gdp_china', 'gdp_europe','Celsius', 'CO2_Concentration']])
lag_order = select_order(data=quick_analysis_ln, maxlags=10, deterministic="ci", seasons=4)
lag_order.summary()
rank_test = select_coint_rank(quick_analysis_ln, 0, 3, method="trace",signif=0.05)
rank_test.rank
model = VECM(quick_analysis_ln, deterministic="ci", seasons=4,
k_ar_diff=lag_order.aic, # =3
coint_rank=rank_test.rank) # =1
vecm_res = model.fit()
vecm_res.summary()
fixing_index = []
fixing_value = []
for i in range(len(co2_conc)):
if co2_conc.iloc[i].average == -999.99:
y = co2_conc.iloc[i].year
a = co2_conc[(co2_conc.year == y) & (co2_conc.average != -999.99)]['average'].values.mean()
fixing_index.append(i)
fixing_value.append(a)
else:
fixing_value.append(co2_conc.iloc[i].average)
co2_conc['average'] = fixing_value
co2_conc_avg = co2_conc.groupby('year').average.mean()
co2_conc_summary = pd.concat([co2_conc_avg,co2_conc.groupby('year').average.std()*np.sqrt(12)],axis = 1)
co2_conc_summary.columns = ['yearly_mean','yearly_vol']
con2_con_summary = pd.DataFrame(co2_conc_summary)
co2_conc_summary['%incr_mean'] = (co2_conc_summary['yearly_mean'].shift(-1) / co2_conc_summary['yearly_mean'] -1 ).fillna(0)
co2_conc_summary['%incr_vol'] = (co2_conc_summary['yearly_vol'].shift(-1) / co2_conc_summary['yearly_vol'] -1 ).fillna(0)
co2_conc_summary = co2_conc_summary.join(world_co2_em).join(world_co2_em_gas).join(world_co2_em_liq).join(world_co2_em_sol)
co2_analysis = co2_conc_summary.dropna()
# First dif
input_df = (co2_analysis[['yearly_mean','World_Emission_general']].apply(np.log) - co2_analysis[['yearly_mean','World_Emission_general']].apply(np.log).shift(1)).dropna()
reg_model = sm.OLS(input_df['yearly_mean'],input_df['World_Emission_general'])
result = reg_model.fit()
print(result.summary())
r, p = stats.pearsonr(input_df['yearly_mean'],input_df['World_Emission_general'])
print(f"Scipy computed Pearson r: {r} and p-value: {p}")
fig,ax1 = plt.subplots(figsize = (15,8))
color = 'tab:red'
ax1.plot(input_df.index, input_df['yearly_mean'], color = color)
ax1.tick_params(axis = 'y',labelcolor = color)
ax1.set_xlabel('time (year)')
ax1.set_ylabel('First difference of yearly average CO2 concentration', color = color, size = 15)
ax2 = ax1.twinx() # instantiate a second axes that shares the same x-axis
color = color = 'tab:blue'
ax2.plot(input_df.index, input_df['World_Emission_general'], color = color)
ax2.tick_params(axis = 'y',labelcolor = color)
ax2.set_ylabel('First difference of CO2 emission', color = color, size = 15)
fig.tight_layout() # otherwise the right y-label is slightly clipped
plt.title("First difference of CO2 concentration and Emission", size = 20)
plt.show()
#1998 check trend
input_df_reg = input_df.copy()
input_df_reg['positive_change'] = list(map(lambda x : 0 if x <= 0 else x,input_df['World_Emission_general'] ))
input_df_reg['negative_change'] = list(map(lambda x : 0 if x >= 0 else x,input_df['World_Emission_general'] ))
input_df_reg['lag1'] = input_df['World_Emission_general'].shift(1)
input_df_reg['lag2'] = input_df['World_Emission_general'].shift(2)
input_df_reg['lag3'] = input_df['World_Emission_general'].shift(3)
input_df_reg = input_df_reg.dropna()
#x = input_df_reg[['positive_change','negative_change','lag1','lag2']] #,'lag3']]
#x = input_df_reg[['World_Emission_general','positive_change','negative_change','lag1','lag2','lag3']]
x = input_df_reg[['World_Emission_general','lag1','lag2','lag3']]
reg_model = sm.OLS(input_df_reg['yearly_mean'],x)
result = reg_model.fit()
print(result.summary())
## Three years of emissions
# test perfect number of lags used in model with positive/negative change in emissions
nlag = 10
for i in range(nlag+1):
#print(i)
input_df_reg_loop = input_df.copy()
input_df_reg_loop['positive_change'] = list(map(lambda x : 0 if x <= 0 else x,input_df['World_Emission_general'] ))
input_df_reg_loop['negative_change'] = list(map(lambda x : 0 if x >= 0 else x,input_df['World_Emission_general'] ))
#running = i.copy()
column_names = ['positive_change','negative_change']
print("Number of lag : ", str(i))
while i > 0:
text = 'lag' + '_' + str(i)
input_df_reg_loop[text] = input_df['World_Emission_general'].shift(i)
column_names.append(text)
i = i - 1
input_df_reg_loop = input_df_reg_loop.dropna()
x = input_df_reg_loop[column_names]
reg_model = sm.OLS(input_df_reg_loop['yearly_mean'],x)
result = reg_model.fit()
print("R-squared : ", result.rsquared)
print("P-value : ")
print(result.pvalues)
print(" ")
oil_co2_df
# Choosing last Month data
year_oil_co2_df = oil_co2_df.groupby('Year').last().copy()
#Joining data to create big dataframe - yearly data
y_glob_temp = glob_temp.copy()
index = y_glob_temp.index
year_index = []
for i in index:
year_index.append(str(i)[:4])
y_glob_temp['Year'] = [int(i) for i in year_index]
y_glob_temp = y_glob_temp.set_index('Year')
y_glob_temp = y_glob_temp.groupby('Year').last()
y_gdp = gdp_df.copy()
index = y_gdp.index
year_index = []
for i in index:
year_index.append(str(i)[:4])
y_gdp['Year'] = [int(i) for i in year_index]
y_gdp = y_gdp.set_index('Year')
y_gdp = y_gdp.groupby('Year').last()
y_analysis_df = year_oil_co2_df.join(y_gdp.drop(columns = ['Month'])).dropna(subset=['gdp_date','gdp_us','gdp_china','gdp_europe'])
y_analysis_df = y_analysis_df.join(y_glob_temp['Celsius'])
#Rename Carbon Dioxide concentration
temp = y_analysis_df['average']
y_analysis_df = y_analysis_df.drop(columns = 'average')
y_analysis_df['CO2_Concentration'] = temp
y_analysis_df = y_analysis_df.drop(columns = 'gdp_date')
y_analysis_df.head()
#Create first difference
temp_frame = (np.log(y_analysis_df[['gdp_china','gdp_us','gdp_europe','Celsius','CO2_Concentration']].shift(0)) - np.log(y_analysis_df[['gdp_china','gdp_us','gdp_europe','Celsius','CO2_Concentration']].shift(1)))
temp_frame = temp_frame.rename(columns = {'gdp_china' : 'chg_gdp_china','gdp_europe':'chg_gdp_europe','gdp_us':'chg_gdp_us','Celsius':'chg_celsius','CO2_Concentration':'chg_co2_concentration'})
y_analysis_df = y_analysis_df.merge(temp_frame, how='inner', on='Year')
y_analysis_df.head()
y_analysis_df = y_analysis_df.iloc[1:]
#Export data to output table
y_analysis_df.to_csv('yearly_analysis_prep_data.csv')
quick_anaysis = y_analysis_df.copy()
# test perfect number of lags used in model
import statsmodels.api as sm
nlag = 20
for i in range(nlag+1):
#print(i)
quick_anaysis_df_loop = quick_anaysis.copy()
#running = i.copy()
column_names = ['chg_oil_spot_WTI_Positive','chg_oil_spot_WTI_Negative','chg_gdp_us','chg_gdp_china','chg_gdp_europe']
print("Number of lag : ", str(i))
while i > 0:
text = 'lag' + '_' + str(i)
quick_anaysis_df_loop[text] = quick_anaysis['oil_spot_WTI'].shift(i)
quick_anaysis_df_loop['chg_oil_spot_WTI' + "_" + text] = quick_anaysis['oil_spot_WTI'].shift(i)
quick_anaysis_df_loop['chg_gdp_us' + "_" + text] = quick_anaysis['gdp_us'].shift(i)
quick_anaysis_df_loop['chg_gdp_china' + "_" + text] = quick_anaysis['gdp_china'].shift(i)
quick_anaysis_df_loop['chg_gdp_europe' + "_" + text] = quick_anaysis['gdp_europe'].shift(i)
column_names.append('chg_oil_spot_WTI' + "_" + text)
column_names.append('chg_gdp_us' + "_" + text)
column_names.append('chg_gdp_china' + "_" + text)
column_names.append('chg_gdp_europe' + "_" + text)
i = i - 1
#print(i+1)
#print(" ")
quick_anaysis_df_loop = quick_anaysis_df_loop.dropna()
x = quick_anaysis_df_loop[column_names]
reg_model = sm.OLS(quick_anaysis_df_loop['chg_co2_concentration'],x)
result = reg_model.fit()
print("R-squared : ", result.rsquared)
print("P-value : ")
print(round(result.pvalues,4))
print(" ")
dummy_list = oil_co2_df['average']
oil_co2_df.drop(columns = 'average')
oil_co2_df['co2_conc'] = dummy_list
first_dif_conc = (np.log(oil_co2_df['co2_conc'].shift(1)) - np.log(oil_co2_df['co2_conc'].shift(0)))
oil_co2_df['chg_co2_conc'] = first_dif_conc
import matplotlib.pyplot as plt
fig,ax1 = plt.subplots(figsize = (15,8))
ax1.plot(oil_co2_df.Date,oil_co2_df['average'],color = 'tab:red')
ax2 = ax1.twinx()
ax2.plot(oil_co2_df.Date,oil_co2_df[ 'oil_spot_WTI'],color = 'tab:blue')
import matplotlib.pyplot as plt
fig,ax1 = plt.subplots(figsize = (15,8))
ax1.plot(oil_co2_df.Date,oil_co2_df['chg_oil_spot_WTI'],color = 'tab:red')
ax2 = ax1.twinx()
ax2.plot(oil_co2_df.Date,oil_co2_df['chg_co2_conc'],color = 'tab:blue')
# test perfect number of lags used in model
import statsmodels.api as sm
nlag = 20
for i in range(nlag+1):
#print(i)
oil_co2_df_loop = oil_co2_df.copy()
#running = i.copy()
column_names = ['chg_oil_spot_WTI']
print("Number of lag : ", str(i))
while i > 0:
text = 'lag' + '_' + str(i)
oil_co2_df_loop[text] = oil_co2_df['chg_oil_spot_WTI'].shift(i)
column_names.append(text)
i = i - 1
#print(i+1)
#print(" ")
oil_co2_df_loop = oil_co2_df_loop.dropna()
x = oil_co2_df_loop[column_names]
reg_model = sm.OLS(oil_co2_df_loop['chg_co2_conc'],x)
result = reg_model.fit()
print("R-squared : ", result.rsquared)
print("P-value : ")
print(result.pvalues)
print(" ")
#Weekly gasoline
analysis = oil_co2_df.copy()
oil_co2_df.columns
analysis = analysis[['chg_oil_spot_WTI_Positive','chg_oil_spot_WTI_Negative','chg_Gasoline_price_US','chg_co2_conc']].dropna(subset = ['chg_co2_conc'])
### Weekly ###
analysis_reg = analysis.copy()
#input_df_reg['positive_change'] = list(map(lambda x : 0 if x <= 0 else x,input_df['World_Emission_general'] ))
#input_df_reg['negative_change'] = list(map(lambda x : 0 if x >= 0 else x,input_df['World_Emission_general'] ))
analysis_reg['p+_lag1'] = analysis['chg_oil_spot_WTI_Positive'].shift(1)
analysis_reg['p+_lag2'] = analysis['chg_oil_spot_WTI_Positive'].shift(2)
analysis_reg['p+_lag3'] = analysis['chg_oil_spot_WTI_Positive'].shift(3)
analysis_reg['p+_lag4'] = analysis['chg_oil_spot_WTI_Positive'].shift(4)
analysis_reg['p+_lag5'] = analysis['chg_oil_spot_WTI_Positive'].shift(5)
analysis_reg['p+_lag6'] = analysis['chg_oil_spot_WTI_Positive'].shift(6)
analysis_reg['p+_lag7'] = analysis['chg_oil_spot_WTI_Positive'].shift(7)
analysis_reg['p+_lag8'] = analysis['chg_oil_spot_WTI_Positive'].shift(8)
analysis_reg['p+_lag9'] = analysis['chg_oil_spot_WTI_Positive'].shift(9)
analysis_reg['p+_lag10'] = analysis['chg_oil_spot_WTI_Positive'].shift(10)
analysis_reg['p-_lag1'] = analysis['chg_oil_spot_WTI_Negative'].shift(1)
analysis_reg['p-_lag2'] = analysis['chg_oil_spot_WTI_Negative'].shift(2)
analysis_reg['p-_lag3'] = analysis['chg_oil_spot_WTI_Negative'].shift(3)
analysis_reg['p-_lag4'] = analysis['chg_oil_spot_WTI_Negative'].shift(4)
analysis_reg['p-_lag5'] = analysis['chg_oil_spot_WTI_Negative'].shift(5)
analysis_reg['p-_lag6'] = analysis['chg_oil_spot_WTI_Negative'].shift(6)
analysis_reg['p-_lag7'] = analysis['chg_oil_spot_WTI_Negative'].shift(7)
analysis_reg['p-_lag8'] = analysis['chg_oil_spot_WTI_Negative'].shift(8)
analysis_reg['p-_lag9'] = analysis['chg_oil_spot_WTI_Negative'].shift(9)
analysis_reg['p-_lag10'] = analysis['chg_oil_spot_WTI_Negative'].shift(10)
analysis_reg = analysis_reg.dropna()
#Weekly gasoline
x = analysis_reg[['chg_oil_spot_WTI_Positive', 'chg_oil_spot_WTI_Negative','p+_lag1', 'p+_lag2',
'p+_lag3', 'p+_lag4', 'p+_lag5', 'p+_lag6', 'p+_lag7', 'p+_lag8',
'p+_lag9', 'p+_lag10', 'p-_lag1', 'p-_lag2', 'p-_lag3', 'p-_lag4',
'p-_lag5', 'p-_lag6', 'p-_lag7', 'p-_lag8', 'p-_lag9', 'p-_lag10']]
reg_model = sm.OLS(analysis_reg['chg_Gasoline_price_US'],x)
result = reg_model.fit()
print(result.summary())
## Three years of emissions
result.tvalues.to_frame()
#Weekly
analysis = oil_co2_df.copy()
oil_co2_df.columns
analysis = analysis[['chg_oil_spot_WTI_Positive','chg_oil_spot_WTI_Negative','chg_Gasoline_price_US','chg_co2_conc']].dropna(subset = ['chg_co2_conc'])
### Weekly ###
analysis_reg = analysis.copy()
#input_df_reg['positive_change'] = list(map(lambda x : 0 if x <= 0 else x,input_df['World_Emission_general'] ))
#input_df_reg['negative_change'] = list(map(lambda x : 0 if x >= 0 else x,input_df['World_Emission_general'] ))
analysis_reg['p+_lag1'] = analysis['chg_oil_spot_WTI_Positive'].shift(1)
analysis_reg['p+_lag2'] = analysis['chg_oil_spot_WTI_Positive'].shift(2)
analysis_reg['p+_lag3'] = analysis['chg_oil_spot_WTI_Positive'].shift(3)
analysis_reg['p+_lag4'] = analysis['chg_oil_spot_WTI_Positive'].shift(4)
analysis_reg['p+_lag5'] = analysis['chg_oil_spot_WTI_Positive'].shift(5)
analysis_reg['p+_lag6'] = analysis['chg_oil_spot_WTI_Positive'].shift(6)
analysis_reg['p+_lag7'] = analysis['chg_oil_spot_WTI_Positive'].shift(7)
analysis_reg['p+_lag8'] = analysis['chg_oil_spot_WTI_Positive'].shift(8)
analysis_reg['p+_lag9'] = analysis['chg_oil_spot_WTI_Positive'].shift(9)
analysis_reg['p+_lag10'] = analysis['chg_oil_spot_WTI_Positive'].shift(10)
analysis_reg['p-_lag1'] = analysis['chg_oil_spot_WTI_Negative'].shift(1)
analysis_reg['p-_lag2'] = analysis['chg_oil_spot_WTI_Negative'].shift(2)
analysis_reg['p-_lag3'] = analysis['chg_oil_spot_WTI_Negative'].shift(3)
analysis_reg['p-_lag4'] = analysis['chg_oil_spot_WTI_Negative'].shift(4)
analysis_reg['p-_lag5'] = analysis['chg_oil_spot_WTI_Negative'].shift(5)
analysis_reg['p-_lag6'] = analysis['chg_oil_spot_WTI_Negative'].shift(6)
analysis_reg['p-_lag7'] = analysis['chg_oil_spot_WTI_Negative'].shift(7)
analysis_reg['p-_lag8'] = analysis['chg_oil_spot_WTI_Negative'].shift(8)
analysis_reg['p-_lag9'] = analysis['chg_oil_spot_WTI_Negative'].shift(9)
analysis_reg['p-_lag10'] = analysis['chg_oil_spot_WTI_Negative'].shift(10)
analysis_reg = analysis_reg.dropna()
#Weekly CO2
x = analysis_reg[['chg_oil_spot_WTI_Positive', 'chg_oil_spot_WTI_Negative','p+_lag1', 'p+_lag2',
'p+_lag3', 'p+_lag4', 'p+_lag5', 'p+_lag6', 'p+_lag7', 'p+_lag8',
'p+_lag9', 'p+_lag10', 'p-_lag1', 'p-_lag2', 'p-_lag3', 'p-_lag4',
'p-_lag5', 'p-_lag6', 'p-_lag7', 'p-_lag8', 'p-_lag9', 'p-_lag10']]
reg_model = sm.OLS(analysis_reg['chg_co2_conc'],x)
result = reg_model.fit()
print(result.summary())
## Three years of emissions
result.tvalues.to_frame()
#Weekly oil futures to future gasoline price
#Weekly
analysis = oil_co2_df.copy()
oil_co2_df.columns
analysis = analysis[['chg_oil_future_contract1_Positive','chg_oil_future_contract1_Negative','chg_Gasoline_price_US','chg_co2_conc']].dropna(subset = ['chg_co2_conc'])
analysis_reg = analysis.copy()
#input_df_reg['positive_change'] = list(map(lambda x : 0 if x <= 0 else x,input_df['World_Emission_general'] ))
#input_df_reg['negative_change'] = list(map(lambda x : 0 if x >= 0 else x,input_df['World_Emission_general'] ))
analysis_reg['p+_lag1'] = analysis['chg_oil_future_contract1_Positive'].shift(1)
analysis_reg['p+_lag2'] = analysis['chg_oil_future_contract1_Positive'].shift(2)
analysis_reg['p+_lag3'] = analysis['chg_oil_future_contract1_Positive'].shift(3)
analysis_reg['p+_lag4'] = analysis['chg_oil_future_contract1_Positive'].shift(4)
analysis_reg['p+_lag5'] = analysis['chg_oil_future_contract1_Positive'].shift(5)
analysis_reg['p+_lag6'] = analysis['chg_oil_future_contract1_Positive'].shift(6)
analysis_reg['p+_lag7'] = analysis['chg_oil_future_contract1_Positive'].shift(7)
analysis_reg['p+_lag8'] = analysis['chg_oil_future_contract1_Positive'].shift(8)
analysis_reg['p+_lag9'] = analysis['chg_oil_future_contract1_Positive'].shift(9)
analysis_reg['p+_lag10'] = analysis['chg_oil_future_contract1_Positive'].shift(10)
analysis_reg['chg_Future_gasoline'] = analysis['chg_Gasoline_price_US'].shift(-1)
analysis_reg['p-_lag1'] = analysis['chg_oil_future_contract1_Negative'].shift(1)
analysis_reg['p-_lag2'] = analysis['chg_oil_future_contract1_Negative'].shift(2)
analysis_reg['p-_lag3'] = analysis['chg_oil_future_contract1_Negative'].shift(3)
analysis_reg['p-_lag4'] = analysis['chg_oil_future_contract1_Negative'].shift(4)
analysis_reg['p-_lag5'] = analysis['chg_oil_future_contract1_Negative'].shift(5)
analysis_reg['p-_lag6'] = analysis['chg_oil_future_contract1_Negative'].shift(6)
analysis_reg['p-_lag7'] = analysis['chg_oil_future_contract1_Negative'].shift(7)
analysis_reg['p-_lag8'] = analysis['chg_oil_future_contract1_Negative'].shift(8)
analysis_reg['p-_lag9'] = analysis['chg_oil_future_contract1_Negative'].shift(9)
analysis_reg['p-_lag10'] = analysis['chg_oil_future_contract1_Negative'].shift(10)
analysis_reg = analysis_reg.dropna()
#Regression week oil futures to future gasoline price
x = analysis_reg[['chg_oil_future_contract1_Positive', 'chg_oil_future_contract1_Negative','p+_lag1', 'p+_lag2',
'p+_lag3', 'p+_lag4', 'p+_lag5', 'p+_lag6', 'p+_lag7', 'p+_lag8',
'p+_lag9', 'p+_lag10', 'p-_lag1', 'p-_lag2', 'p-_lag3', 'p-_lag4',
'p-_lag5', 'p-_lag6', 'p-_lag7', 'p-_lag8', 'p-_lag9', 'p-_lag10']]
reg_model = sm.OLS(analysis_reg['chg_Future_gasoline'],x)
result = reg_model.fit()
print(result.summary())
## Three years of emissions
result.tvalues.to_frame()
### Weekly ###
### Weekly Lag 0###
analysis = oil_co2_df.copy()
analysis = analysis[['chg_oil_spot_WTI_Positive','chg_oil_spot_WTI_Negative','chg_Gasoline_price_US','chg_co2_conc']].dropna(subset = ['chg_co2_conc'])
analysis_reg = analysis.copy()
analysis_reg = analysis_reg
x = analysis_reg[['chg_oil_spot_WTI_Positive', 'chg_oil_spot_WTI_Negative']]
reg_model = sm.OLS(analysis_reg['chg_Gasoline_price_US'],x)
result = reg_model.fit()
print(result.summary())
### Weekly ###
### Weekly Lag 6###
analysis = oil_co2_df.copy()
analysis = analysis[['chg_oil_spot_WTI_Positive','chg_oil_spot_WTI_Negative','chg_Gasoline_price_US','chg_co2_conc']].dropna(subset = ['chg_co2_conc'])
analysis_reg = analysis.copy()
analysis_reg['p+_lag1'] = analysis['chg_oil_spot_WTI_Positive'].shift(1)
analysis_reg['p+_lag2'] = analysis['chg_oil_spot_WTI_Positive'].shift(2)
analysis_reg['p+_lag3'] = analysis['chg_oil_spot_WTI_Positive'].shift(3)
analysis_reg['p+_lag4'] = analysis['chg_oil_spot_WTI_Positive'].shift(4)
analysis_reg['p+_lag5'] = analysis['chg_oil_spot_WTI_Positive'].shift(5)
analysis_reg['p+_lag6'] = analysis['chg_oil_spot_WTI_Positive'].shift(6)
analysis_reg['p-_lag1'] = analysis['chg_oil_spot_WTI_Negative'].shift(1)
analysis_reg['p-_lag2'] = analysis['chg_oil_spot_WTI_Negative'].shift(2)
analysis_reg['p-_lag3'] = analysis['chg_oil_spot_WTI_Negative'].shift(3)
analysis_reg['p-_lag4'] = analysis['chg_oil_spot_WTI_Negative'].shift(4)
analysis_reg['p-_lag5'] = analysis['chg_oil_spot_WTI_Negative'].shift(5)
analysis_reg['p-_lag6'] = analysis['chg_oil_spot_WTI_Negative'].shift(6)
analysis_reg = analysis_reg.dropna(subset = ['p+_lag1','p-_lag1','p+_lag2','p-_lag2','p+_lag3','p-_lag3','p+_lag4','p-_lag4','p+_lag5','p-_lag5','p+_lag6','p-_lag6'])
x = analysis_reg[['chg_oil_spot_WTI_Positive', 'chg_oil_spot_WTI_Negative','p+_lag1','p-_lag1','p+_lag2','p-_lag2','p+_lag3','p-_lag3','p+_lag4','p-_lag4','p+_lag5','p-_lag5','p+_lag6','p-_lag6']]
reg_model = sm.OLS(analysis_reg['chg_Gasoline_price_US'],x)
result = reg_model.fit()
print(result.summary())
### Weekly ###
### Weekly Lag 12###
analysis = oil_co2_df.copy()
analysis = analysis[['chg_oil_spot_WTI_Positive','chg_oil_spot_WTI_Negative','chg_Gasoline_price_US','chg_co2_conc']].dropna(subset = ['chg_co2_conc'])
analysis_reg = analysis.copy()
analysis_reg['p+_lag1'] = analysis['chg_oil_spot_WTI_Positive'].shift(1)
analysis_reg['p+_lag2'] = analysis['chg_oil_spot_WTI_Positive'].shift(2)
analysis_reg['p+_lag3'] = analysis['chg_oil_spot_WTI_Positive'].shift(3)
analysis_reg['p+_lag4'] = analysis['chg_oil_spot_WTI_Positive'].shift(4)
analysis_reg['p+_lag5'] = analysis['chg_oil_spot_WTI_Positive'].shift(5)
analysis_reg['p+_lag6'] = analysis['chg_oil_spot_WTI_Positive'].shift(6)
analysis_reg['p+_lag7'] = analysis['chg_oil_spot_WTI_Positive'].shift(7)
analysis_reg['p+_lag8'] = analysis['chg_oil_spot_WTI_Positive'].shift(8)
analysis_reg['p+_lag9'] = analysis['chg_oil_spot_WTI_Positive'].shift(9)
analysis_reg['p+_lag10'] = analysis['chg_oil_spot_WTI_Positive'].shift(10)
analysis_reg['p+_lag11'] = analysis['chg_oil_spot_WTI_Positive'].shift(11)
analysis_reg['p+_lag12'] = analysis['chg_oil_spot_WTI_Positive'].shift(12)
analysis_reg['p-_lag1'] = analysis['chg_oil_spot_WTI_Negative'].shift(1)
analysis_reg['p-_lag2'] = analysis['chg_oil_spot_WTI_Negative'].shift(2)
analysis_reg['p-_lag3'] = analysis['chg_oil_spot_WTI_Negative'].shift(3)
analysis_reg['p-_lag4'] = analysis['chg_oil_spot_WTI_Negative'].shift(4)
analysis_reg['p-_lag5'] = analysis['chg_oil_spot_WTI_Negative'].shift(5)
analysis_reg['p-_lag6'] = analysis['chg_oil_spot_WTI_Negative'].shift(6)
analysis_reg['p-_lag7'] = analysis['chg_oil_spot_WTI_Negative'].shift(7)
analysis_reg['p-_lag8'] = analysis['chg_oil_spot_WTI_Negative'].shift(8)
analysis_reg['p-_lag9'] = analysis['chg_oil_spot_WTI_Negative'].shift(9)
analysis_reg['p-_lag10'] = analysis['chg_oil_spot_WTI_Negative'].shift(10)
analysis_reg['p-_lag11'] = analysis['chg_oil_spot_WTI_Negative'].shift(11)
analysis_reg['p-_lag12'] = analysis['chg_oil_spot_WTI_Negative'].shift(12)
analysis_reg = analysis_reg.dropna(subset = ['p+_lag1','p-_lag1','p+_lag2','p-_lag2','p+_lag3','p-_lag3','p+_lag4','p-_lag4','p+_lag5','p-_lag5','p+_lag6','p-_lag6','p+_lag7','p-_lag7','p+_lag8','p-_lag8','p+_lag9','p-_lag9','p+_lag10','p-_lag10','p+_lag11','p-_lag11','p+_lag12','p-_lag12'])
x = analysis_reg[['chg_oil_spot_WTI_Positive', 'chg_oil_spot_WTI_Negative','p+_lag1','p-_lag1','p+_lag2','p-_lag2','p+_lag3','p-_lag3','p+_lag4','p-_lag4','p+_lag5','p-_lag5','p+_lag6','p-_lag6','p+_lag7','p-_lag7','p+_lag8','p-_lag8','p+_lag9','p-_lag9','p+_lag10','p-_lag10','p+_lag11','p-_lag11','p+_lag12','p-_lag12']]
reg_model = sm.OLS(analysis_reg['chg_Gasoline_price_US'],x)
result = reg_model.fit()
print(result.summary())
### Weekly ###
### Weekly Lag 24###
analysis = oil_co2_df.copy()
analysis = analysis[['chg_oil_spot_WTI_Positive','chg_oil_spot_WTI_Negative','chg_Gasoline_price_US','chg_co2_conc']].dropna(subset = ['chg_co2_conc'])
analysis_reg = analysis.copy()
analysis_reg['p+_lag1'] = analysis['chg_oil_spot_WTI_Positive'].shift(1)
analysis_reg['p+_lag2'] = analysis['chg_oil_spot_WTI_Positive'].shift(2)
analysis_reg['p+_lag3'] = analysis['chg_oil_spot_WTI_Positive'].shift(3)
analysis_reg['p+_lag4'] = analysis['chg_oil_spot_WTI_Positive'].shift(4)
analysis_reg['p+_lag5'] = analysis['chg_oil_spot_WTI_Positive'].shift(5)
analysis_reg['p+_lag6'] = analysis['chg_oil_spot_WTI_Positive'].shift(6)
analysis_reg['p+_lag7'] = analysis['chg_oil_spot_WTI_Positive'].shift(7)
analysis_reg['p+_lag8'] = analysis['chg_oil_spot_WTI_Positive'].shift(8)
analysis_reg['p+_lag9'] = analysis['chg_oil_spot_WTI_Positive'].shift(9)
analysis_reg['p+_lag10'] = analysis['chg_oil_spot_WTI_Positive'].shift(10)
analysis_reg['p+_lag11'] = analysis['chg_oil_spot_WTI_Positive'].shift(11)
analysis_reg['p+_lag12'] = analysis['chg_oil_spot_WTI_Positive'].shift(12)
analysis_reg['p+_lag13'] = analysis['chg_oil_spot_WTI_Positive'].shift(13)
analysis_reg['p+_lag14'] = analysis['chg_oil_spot_WTI_Positive'].shift(14)
analysis_reg['p+_lag15'] = analysis['chg_oil_spot_WTI_Positive'].shift(15)
analysis_reg['p+_lag16'] = analysis['chg_oil_spot_WTI_Positive'].shift(16)
analysis_reg['p+_lag17'] = analysis['chg_oil_spot_WTI_Positive'].shift(17)
analysis_reg['p+_lag18'] = analysis['chg_oil_spot_WTI_Positive'].shift(18)
analysis_reg['p+_lag19'] = analysis['chg_oil_spot_WTI_Positive'].shift(19)
analysis_reg['p+_lag20'] = analysis['chg_oil_spot_WTI_Positive'].shift(20)
analysis_reg['p+_lag21'] = analysis['chg_oil_spot_WTI_Positive'].shift(21)
analysis_reg['p+_lag22'] = analysis['chg_oil_spot_WTI_Positive'].shift(22)
analysis_reg['p+_lag23'] = analysis['chg_oil_spot_WTI_Positive'].shift(23)
analysis_reg['p+_lag24'] = analysis['chg_oil_spot_WTI_Positive'].shift(24)
analysis_reg['p-_lag1'] = analysis['chg_oil_spot_WTI_Negative'].shift(1)
analysis_reg['p-_lag2'] = analysis['chg_oil_spot_WTI_Negative'].shift(2)
analysis_reg['p-_lag3'] = analysis['chg_oil_spot_WTI_Negative'].shift(3)
analysis_reg['p-_lag4'] = analysis['chg_oil_spot_WTI_Negative'].shift(4)
analysis_reg['p-_lag5'] = analysis['chg_oil_spot_WTI_Negative'].shift(5)
analysis_reg['p-_lag6'] = analysis['chg_oil_spot_WTI_Negative'].shift(6)
analysis_reg['p-_lag7'] = analysis['chg_oil_spot_WTI_Negative'].shift(7)
analysis_reg['p-_lag8'] = analysis['chg_oil_spot_WTI_Negative'].shift(8)
analysis_reg['p-_lag9'] = analysis['chg_oil_spot_WTI_Negative'].shift(9)
analysis_reg['p-_lag10'] = analysis['chg_oil_spot_WTI_Negative'].shift(10)
analysis_reg['p-_lag11'] = analysis['chg_oil_spot_WTI_Negative'].shift(11)
analysis_reg['p-_lag12'] = analysis['chg_oil_spot_WTI_Negative'].shift(12)
analysis_reg['p-_lag13'] = analysis['chg_oil_spot_WTI_Negative'].shift(13)
analysis_reg['p-_lag14'] = analysis['chg_oil_spot_WTI_Negative'].shift(14)
analysis_reg['p-_lag15'] = analysis['chg_oil_spot_WTI_Negative'].shift(15)
analysis_reg['p-_lag16'] = analysis['chg_oil_spot_WTI_Negative'].shift(16)
analysis_reg['p-_lag17'] = analysis['chg_oil_spot_WTI_Negative'].shift(17)
analysis_reg['p-_lag18'] = analysis['chg_oil_spot_WTI_Negative'].shift(18)
analysis_reg['p-_lag19'] = analysis['chg_oil_spot_WTI_Negative'].shift(19)
analysis_reg['p-_lag20'] = analysis['chg_oil_spot_WTI_Negative'].shift(20)
analysis_reg['p-_lag21'] = analysis['chg_oil_spot_WTI_Negative'].shift(21)
analysis_reg['p-_lag22'] = analysis['chg_oil_spot_WTI_Negative'].shift(22)
analysis_reg['p-_lag23'] = analysis['chg_oil_spot_WTI_Negative'].shift(23)
analysis_reg['p-_lag24'] = analysis['chg_oil_spot_WTI_Negative'].shift(24)
analysis_reg = analysis_reg.dropna(subset = ['p+_lag1','p-_lag1','p+_lag2','p-_lag2','p+_lag3','p-_lag3','p+_lag4','p-_lag4','p+_lag5','p-_lag5','p+_lag6','p-_lag6','p+_lag7','p-_lag7','p+_lag8','p-_lag8','p+_lag9','p-_lag9','p+_lag10','p-_lag10','p+_lag11','p-_lag11','p+_lag12','p-_lag12'
,'p+_lag13','p-_lag13','p+_lag14','p-_lag14','p+_lag15','p-_lag15','p+_lag16','p-_lag16','p+_lag17','p-_lag17','p+_lag18','p-_lag18','p+_lag19','p-_lag19','p+_lag20','p-_lag20','p+_lag21','p-_lag21','p+_lag22','p-_lag22','p+_lag23','p-_lag23','p+_lag24','p-_lag24'])
x = analysis_reg[['chg_oil_spot_WTI_Positive', 'chg_oil_spot_WTI_Negative','p+_lag1','p-_lag1','p+_lag2','p-_lag2','p+_lag3','p-_lag3','p+_lag4','p-_lag4','p+_lag5','p-_lag5','p+_lag6','p-_lag6','p+_lag7','p-_lag7','p+_lag8','p-_lag8','p+_lag9','p-_lag9','p+_lag10','p-_lag10','p+_lag11','p-_lag11','p+_lag12','p-_lag12'
,'p+_lag13','p-_lag13','p+_lag14','p-_lag14','p+_lag15','p-_lag15','p+_lag16','p-_lag16','p+_lag17','p-_lag17','p+_lag18','p-_lag18','p+_lag19','p-_lag19','p+_lag20','p-_lag20','p+_lag21','p-_lag21','p+_lag22','p-_lag22','p+_lag23','p-_lag23','p+_lag24','p-_lag24']]
reg_model = sm.OLS(analysis_reg['chg_Gasoline_price_US'],x)
result = reg_model.fit()
print(result.summary())
# test perfect number of lags used in model
import statsmodels.api as sm
nlag = 2
analysis = oil_co2_df.copy()
for i in [2]: #range(nlag+1):
#print(i)
# analysis = oil_co2_df.copy()
analysis = analysis[['chg_oil_spot_WTI_Positive','chg_oil_spot_WTI_Negative','chg_Gasoline_price_US','chg_co2_conc']].dropna(subset = ['chg_co2_conc'])
analysis_reg = analysis.copy()
#running = i.copy()
column_names = ['chg_oil_spot_WTI_Positive','chg_oil_spot_WTI_Negative']
print("Number of lag : ", str(i))
while i > 0:
text1 = 'chg_oil_spot_WTI_Positive' + '_lag' + '_' + str(i)
analysis_reg[text1] = oil_co2_df['chg_oil_spot_WTI_Positive'].shift(i)
column_names.append(text1)
text2 = 'chg_oil_spot_WTI_Negative' + '_lag' + '_' + str(i)
analysis_reg[text2] = oil_co2_df['chg_oil_spot_WTI_Negative'].shift(i)
column_names.append(text2)
i = i - 1
#print(i+1)
#print(" ")
analysis_reg = analysis_reg.dropna(subset = column_names)
x = analysis_reg[column_names][1:][['chg_oil_spot_WTI_Positive','chg_oil_spot_WTI_Negative','chg_oil_spot_WTI_Positive_lag_1','chg_oil_spot_WTI_Negative_lag_1','chg_oil_spot_WTI_Positive_lag_2','chg_oil_spot_WTI_Negative_lag_2']]
reg_model = sm.OLS(analysis_reg['chg_Gasoline_price_US'][1:],x)
result = reg_model.fit()
print("Adj. R-squared : ", result.rsquared_adj)
print("P-value : ")
print(result.summary())
#print(result.pvalues)
print(" ")
### Weekly CO2 ###
### Weekly Lag 0###
analysis = oil_co2_df.copy()
analysis = analysis[['chg_oil_spot_WTI_Positive','chg_oil_spot_WTI_Negative','chg_Gasoline_price_US','chg_co2_conc']].dropna(subset = ['chg_co2_conc'])
analysis_reg = analysis.copy()
analysis_reg = analysis_reg
x = analysis_reg[['chg_oil_spot_WTI_Positive', 'chg_oil_spot_WTI_Negative']]
reg_model = sm.OLS(analysis_reg['chg_co2_conc'],x)
result = reg_model.fit()
print(result.summary())
### Weekly ###
### Weekly Lag 3###
analysis = oil_co2_df.copy()
analysis = analysis[['chg_oil_spot_WTI_Positive','chg_oil_spot_WTI_Negative','chg_Gasoline_price_US','chg_co2_conc']].dropna(subset = ['chg_co2_conc'])
analysis_reg = analysis.copy()
analysis_reg['p+_lag1'] = analysis['chg_oil_spot_WTI_Positive'].shift(1)
analysis_reg['p+_lag2'] = analysis['chg_oil_spot_WTI_Positive'].shift(2)
analysis_reg['p+_lag3'] = analysis['chg_oil_spot_WTI_Positive'].shift(3)
analysis_reg['p-_lag1'] = analysis['chg_oil_spot_WTI_Negative'].shift(1)
analysis_reg['p-_lag2'] = analysis['chg_oil_spot_WTI_Negative'].shift(2)
analysis_reg['p-_lag3'] = analysis['chg_oil_spot_WTI_Negative'].shift(3)
analysis_reg = analysis_reg.dropna(subset = ['p+_lag1','p-_lag1','p+_lag2','p-_lag2','p+_lag3','p-_lag3'])
x = analysis_reg[['chg_oil_spot_WTI_Positive', 'chg_oil_spot_WTI_Negative','p+_lag1','p-_lag1','p+_lag2','p-_lag2','p+_lag3','p-_lag3']]
reg_model = sm.OLS(analysis_reg['chg_co2_conc'],x)
result = reg_model.fit()
print(result.summary())
### Weekly ###
### Weekly Lag 6###
analysis = oil_co2_df.copy()
analysis = analysis[['chg_oil_spot_WTI_Positive','chg_oil_spot_WTI_Negative','chg_Gasoline_price_US','chg_co2_conc']].dropna(subset = ['chg_co2_conc'])
analysis_reg = analysis.copy()
analysis_reg['p+_lag1'] = analysis['chg_oil_spot_WTI_Positive'].shift(1)
analysis_reg['p+_lag2'] = analysis['chg_oil_spot_WTI_Positive'].shift(2)
analysis_reg['p+_lag3'] = analysis['chg_oil_spot_WTI_Positive'].shift(3)
analysis_reg['p+_lag4'] = analysis['chg_oil_spot_WTI_Positive'].shift(4)
analysis_reg['p+_lag5'] = analysis['chg_oil_spot_WTI_Positive'].shift(5)
analysis_reg['p+_lag6'] = analysis['chg_oil_spot_WTI_Positive'].shift(6)
analysis_reg['p-_lag1'] = analysis['chg_oil_spot_WTI_Negative'].shift(1)
analysis_reg['p-_lag2'] = analysis['chg_oil_spot_WTI_Negative'].shift(2)
analysis_reg['p-_lag3'] = analysis['chg_oil_spot_WTI_Negative'].shift(3)
analysis_reg['p-_lag4'] = analysis['chg_oil_spot_WTI_Negative'].shift(4)
analysis_reg['p-_lag5'] = analysis['chg_oil_spot_WTI_Negative'].shift(5)
analysis_reg['p-_lag6'] = analysis['chg_oil_spot_WTI_Negative'].shift(6)
analysis_reg = analysis_reg.dropna(subset = ['p+_lag1','p-_lag1','p+_lag2','p-_lag2','p+_lag3','p-_lag3','p+_lag4','p-_lag4','p+_lag5','p-_lag5','p+_lag6','p-_lag6'])
x = analysis_reg[['chg_oil_spot_WTI_Positive', 'chg_oil_spot_WTI_Negative','p+_lag1','p-_lag1','p+_lag2','p-_lag2','p+_lag3','p-_lag3','p+_lag4','p-_lag4','p+_lag5','p-_lag5','p+_lag6','p-_lag6']]
reg_model = sm.OLS(analysis_reg['chg_co2_conc'],x)
result = reg_model.fit()
print(result.summary())
### Weekly ###
### Weekly Lag 12###
analysis = oil_co2_df.copy()
analysis = analysis[['chg_oil_spot_WTI_Positive','chg_oil_spot_WTI_Negative','chg_Gasoline_price_US','chg_co2_conc']].dropna(subset = ['chg_co2_conc'])
analysis_reg = analysis.copy()
analysis_reg['p+_lag1'] = analysis['chg_oil_spot_WTI_Positive'].shift(1)
analysis_reg['p+_lag2'] = analysis['chg_oil_spot_WTI_Positive'].shift(2)
analysis_reg['p+_lag3'] = analysis['chg_oil_spot_WTI_Positive'].shift(3)
analysis_reg['p+_lag4'] = analysis['chg_oil_spot_WTI_Positive'].shift(4)
analysis_reg['p+_lag5'] = analysis['chg_oil_spot_WTI_Positive'].shift(5)
analysis_reg['p+_lag6'] = analysis['chg_oil_spot_WTI_Positive'].shift(6)
analysis_reg['p+_lag7'] = analysis['chg_oil_spot_WTI_Positive'].shift(7)
analysis_reg['p+_lag8'] = analysis['chg_oil_spot_WTI_Positive'].shift(8)
analysis_reg['p+_lag9'] = analysis['chg_oil_spot_WTI_Positive'].shift(9)
analysis_reg['p+_lag10'] = analysis['chg_oil_spot_WTI_Positive'].shift(10)
analysis_reg['p+_lag11'] = analysis['chg_oil_spot_WTI_Positive'].shift(11)
analysis_reg['p+_lag12'] = analysis['chg_oil_spot_WTI_Positive'].shift(12)
analysis_reg['p-_lag1'] = analysis['chg_oil_spot_WTI_Negative'].shift(1)
analysis_reg['p-_lag2'] = analysis['chg_oil_spot_WTI_Negative'].shift(2)
analysis_reg['p-_lag3'] = analysis['chg_oil_spot_WTI_Negative'].shift(3)
analysis_reg['p-_lag4'] = analysis['chg_oil_spot_WTI_Negative'].shift(4)
analysis_reg['p-_lag5'] = analysis['chg_oil_spot_WTI_Negative'].shift(5)
analysis_reg['p-_lag6'] = analysis['chg_oil_spot_WTI_Negative'].shift(6)
analysis_reg['p-_lag7'] = analysis['chg_oil_spot_WTI_Negative'].shift(7)
analysis_reg['p-_lag8'] = analysis['chg_oil_spot_WTI_Negative'].shift(8)
analysis_reg['p-_lag9'] = analysis['chg_oil_spot_WTI_Negative'].shift(9)
analysis_reg['p-_lag10'] = analysis['chg_oil_spot_WTI_Negative'].shift(10)
analysis_reg['p-_lag11'] = analysis['chg_oil_spot_WTI_Negative'].shift(11)
analysis_reg['p-_lag12'] = analysis['chg_oil_spot_WTI_Negative'].shift(12)
analysis_reg = analysis_reg.dropna(subset = ['p+_lag1','p-_lag1','p+_lag2','p-_lag2','p+_lag3','p-_lag3','p+_lag4','p-_lag4','p+_lag5','p-_lag5','p+_lag6','p-_lag6','p+_lag7','p-_lag7','p+_lag8','p-_lag8','p+_lag9','p-_lag9','p+_lag10','p-_lag10','p+_lag11','p-_lag11','p+_lag12','p-_lag12'])
x = analysis_reg[['chg_oil_spot_WTI_Positive', 'chg_oil_spot_WTI_Negative','p+_lag1','p-_lag1','p+_lag2','p-_lag2','p+_lag3','p-_lag3','p+_lag4','p-_lag4','p+_lag5','p-_lag5','p+_lag6','p-_lag6','p+_lag7','p-_lag7','p+_lag8','p-_lag8','p+_lag9','p-_lag9','p+_lag10','p-_lag10','p+_lag11','p-_lag11','p+_lag12','p-_lag12']]
reg_model = sm.OLS(analysis_reg['chg_co2_conc'],x)
result = reg_model.fit()
print(result.summary())
### Weekly ###
### Weekly Lag 24###
analysis = oil_co2_df.copy()
analysis = analysis[['chg_oil_spot_WTI_Positive','chg_oil_spot_WTI_Negative','chg_Gasoline_price_US','chg_co2_conc']].dropna(subset = ['chg_co2_conc'])
analysis_reg = analysis.copy()
analysis_reg['p+_lag1'] = analysis['chg_oil_spot_WTI_Positive'].shift(1)
analysis_reg['p+_lag2'] = analysis['chg_oil_spot_WTI_Positive'].shift(2)
analysis_reg['p+_lag3'] = analysis['chg_oil_spot_WTI_Positive'].shift(3)
analysis_reg['p+_lag4'] = analysis['chg_oil_spot_WTI_Positive'].shift(4)
analysis_reg['p+_lag5'] = analysis['chg_oil_spot_WTI_Positive'].shift(5)
analysis_reg['p+_lag6'] = analysis['chg_oil_spot_WTI_Positive'].shift(6)
analysis_reg['p+_lag7'] = analysis['chg_oil_spot_WTI_Positive'].shift(7)
analysis_reg['p+_lag8'] = analysis['chg_oil_spot_WTI_Positive'].shift(8)
analysis_reg['p+_lag9'] = analysis['chg_oil_spot_WTI_Positive'].shift(9)
analysis_reg['p+_lag10'] = analysis['chg_oil_spot_WTI_Positive'].shift(10)
analysis_reg['p+_lag11'] = analysis['chg_oil_spot_WTI_Positive'].shift(11)
analysis_reg['p+_lag12'] = analysis['chg_oil_spot_WTI_Positive'].shift(12)
analysis_reg['p+_lag13'] = analysis['chg_oil_spot_WTI_Positive'].shift(13)
analysis_reg['p+_lag14'] = analysis['chg_oil_spot_WTI_Positive'].shift(14)
analysis_reg['p+_lag15'] = analysis['chg_oil_spot_WTI_Positive'].shift(15)
analysis_reg['p+_lag16'] = analysis['chg_oil_spot_WTI_Positive'].shift(16)
analysis_reg['p+_lag17'] = analysis['chg_oil_spot_WTI_Positive'].shift(17)
analysis_reg['p+_lag18'] = analysis['chg_oil_spot_WTI_Positive'].shift(18)
analysis_reg['p+_lag19'] = analysis['chg_oil_spot_WTI_Positive'].shift(19)
analysis_reg['p+_lag20'] = analysis['chg_oil_spot_WTI_Positive'].shift(20)
analysis_reg['p+_lag21'] = analysis['chg_oil_spot_WTI_Positive'].shift(21)
analysis_reg['p+_lag22'] = analysis['chg_oil_spot_WTI_Positive'].shift(22)
analysis_reg['p+_lag23'] = analysis['chg_oil_spot_WTI_Positive'].shift(23)
analysis_reg['p+_lag24'] = analysis['chg_oil_spot_WTI_Positive'].shift(24)
analysis_reg['p-_lag1'] = analysis['chg_oil_spot_WTI_Negative'].shift(1)
analysis_reg['p-_lag2'] = analysis['chg_oil_spot_WTI_Negative'].shift(2)
analysis_reg['p-_lag3'] = analysis['chg_oil_spot_WTI_Negative'].shift(3)
analysis_reg['p-_lag4'] = analysis['chg_oil_spot_WTI_Negative'].shift(4)
analysis_reg['p-_lag5'] = analysis['chg_oil_spot_WTI_Negative'].shift(5)
analysis_reg['p-_lag6'] = analysis['chg_oil_spot_WTI_Negative'].shift(6)
analysis_reg['p-_lag7'] = analysis['chg_oil_spot_WTI_Negative'].shift(7)
analysis_reg['p-_lag8'] = analysis['chg_oil_spot_WTI_Negative'].shift(8)
analysis_reg['p-_lag9'] = analysis['chg_oil_spot_WTI_Negative'].shift(9)
analysis_reg['p-_lag10'] = analysis['chg_oil_spot_WTI_Negative'].shift(10)
analysis_reg['p-_lag11'] = analysis['chg_oil_spot_WTI_Negative'].shift(11)
analysis_reg['p-_lag12'] = analysis['chg_oil_spot_WTI_Negative'].shift(12)
analysis_reg['p-_lag13'] = analysis['chg_oil_spot_WTI_Negative'].shift(13)
analysis_reg['p-_lag14'] = analysis['chg_oil_spot_WTI_Negative'].shift(14)
analysis_reg['p-_lag15'] = analysis['chg_oil_spot_WTI_Negative'].shift(15)
analysis_reg['p-_lag16'] = analysis['chg_oil_spot_WTI_Negative'].shift(16)
analysis_reg['p-_lag17'] = analysis['chg_oil_spot_WTI_Negative'].shift(17)
analysis_reg['p-_lag18'] = analysis['chg_oil_spot_WTI_Negative'].shift(18)
analysis_reg['p-_lag19'] = analysis['chg_oil_spot_WTI_Negative'].shift(19)
analysis_reg['p-_lag20'] = analysis['chg_oil_spot_WTI_Negative'].shift(20)
analysis_reg['p-_lag21'] = analysis['chg_oil_spot_WTI_Negative'].shift(21)
analysis_reg['p-_lag22'] = analysis['chg_oil_spot_WTI_Negative'].shift(22)
analysis_reg['p-_lag23'] = analysis['chg_oil_spot_WTI_Negative'].shift(23)
analysis_reg['p-_lag24'] = analysis['chg_oil_spot_WTI_Negative'].shift(24)
analysis_reg = analysis_reg.dropna(subset = ['p+_lag1','p-_lag1','p+_lag2','p-_lag2','p+_lag3','p-_lag3','p+_lag4','p-_lag4','p+_lag5','p-_lag5','p+_lag6','p-_lag6','p+_lag7','p-_lag7','p+_lag8','p-_lag8','p+_lag9','p-_lag9','p+_lag10','p-_lag10','p+_lag11','p-_lag11','p+_lag12','p-_lag12'
,'p+_lag13','p-_lag13','p+_lag14','p-_lag14','p+_lag15','p-_lag15','p+_lag16','p-_lag16','p+_lag17','p-_lag17','p+_lag18','p-_lag18','p+_lag19','p-_lag19','p+_lag20','p-_lag20','p+_lag21','p-_lag21','p+_lag22','p-_lag22','p+_lag23','p-_lag23','p+_lag24','p-_lag24'])
x = analysis_reg[['chg_oil_spot_WTI_Positive', 'chg_oil_spot_WTI_Negative','p+_lag1','p-_lag1','p+_lag2','p-_lag2','p+_lag3','p-_lag3','p+_lag4','p-_lag4','p+_lag5','p-_lag5','p+_lag6','p-_lag6','p+_lag7','p-_lag7','p+_lag8','p-_lag8','p+_lag9','p-_lag9','p+_lag10','p-_lag10','p+_lag11','p-_lag11','p+_lag12','p-_lag12'
,'p+_lag13','p-_lag13','p+_lag14','p-_lag14','p+_lag15','p-_lag15','p+_lag16','p-_lag16','p+_lag17','p-_lag17','p+_lag18','p-_lag18','p+_lag19','p-_lag19','p+_lag20','p-_lag20','p+_lag21','p-_lag21','p+_lag22','p-_lag22','p+_lag23','p-_lag23','p+_lag24','p-_lag24']]
reg_model = sm.OLS(analysis_reg['chg_co2_conc'],x)
result = reg_model.fit()
print(result.summary())
#Monthly
analysis = oil_co2_df.copy()
oil_co2_df.columns
analysis_month = analysis.groupby('Par_Month').last()[['oil_spot_WTI','Gasoline_Price_US','co2_conc']]
analysis_reg_month = np.log(analysis_month) - np.log(analysis_month.shift(1))
analysis_reg_month.dropna(inplace = True)
analysis_reg_month['co2_conc'] = analysis_reg_month['co2_conc'] - analysis_reg_month['co2_conc'].shift(12)
analysis_reg_month.dropna(inplace = True)
analysis_reg_month
### Month ###
analysis_reg_month_edited = analysis_reg_month.copy()
analysis_reg_month_edited['positive_change'] = list(map(lambda x : 0 if x <= 0 else x,analysis_reg_month['oil_spot_WTI'] ))
analysis_reg_month_edited['negative_change'] = list(map(lambda x : 0 if x >= 0 else x,analysis_reg_month['oil_spot_WTI'] ))
analysis_reg_month_edited['p+_lag1'] = analysis_reg_month_edited['positive_change'].shift(1)
analysis_reg_month_edited['p+_lag2'] = analysis_reg_month_edited['positive_change'].shift(2)
analysis_reg_month_edited['p+_lag3'] = analysis_reg_month_edited['positive_change'].shift(3)
analysis_reg_month_edited['p+_lag4'] = analysis_reg_month_edited['positive_change'].shift(4)
analysis_reg_month_edited['p+_lag5'] = analysis_reg_month_edited['positive_change'].shift(5)
analysis_reg_month_edited['p+_lag6'] = analysis_reg_month_edited['positive_change'].shift(6)
analysis_reg_month_edited['p+_lag7'] = analysis_reg_month_edited['positive_change'].shift(7)
analysis_reg_month_edited['p+_lag8'] = analysis_reg_month_edited['positive_change'].shift(8)
analysis_reg_month_edited['p+_lag9'] = analysis_reg_month_edited['positive_change'].shift(9)
analysis_reg_month_edited['p+_lag10'] = analysis_reg_month_edited['positive_change'].shift(10)
analysis_reg_month_edited['p-_lag1'] = analysis_reg_month_edited['negative_change'].shift(1)
analysis_reg_month_edited['p-_lag2'] = analysis_reg_month_edited['negative_change'].shift(2)
analysis_reg_month_edited['p-_lag3'] = analysis_reg_month_edited['negative_change'].shift(3)
analysis_reg_month_edited['p-_lag4'] = analysis_reg_month_edited['negative_change'].shift(4)
analysis_reg_month_edited['p-_lag5'] = analysis_reg_month_edited['negative_change'].shift(5)
analysis_reg_month_edited['p-_lag6'] = analysis_reg_month_edited['negative_change'].shift(6)
analysis_reg_month_edited['p-_lag7'] = analysis_reg_month_edited['negative_change'].shift(7)
analysis_reg_month_edited['p-_lag8'] = analysis_reg_month_edited['negative_change'].shift(8)
analysis_reg_month_edited['p-_lag9'] = analysis_reg_month_edited['negative_change'].shift(9)
analysis_reg_month_edited['p-_lag10'] = analysis_reg_month_edited['negative_change'].shift(10)
analysis_reg_month_edited = analysis_reg_month_edited.dropna()
analysis_reg_month_edited
#Monthly gas
x = analysis_reg_month_edited[['positive_change', 'negative_change','p+_lag1', 'p+_lag2',
'p+_lag3', 'p+_lag4', 'p+_lag5', 'p+_lag6', 'p+_lag7', 'p+_lag8',
'p+_lag9', 'p+_lag10', 'p-_lag1', 'p-_lag2', 'p-_lag3', 'p-_lag4',
'p-_lag5', 'p-_lag6', 'p-_lag7', 'p-_lag8', 'p-_lag9', 'p-_lag10']]
reg_model = sm.OLS(analysis_reg_month_edited['Gasoline_Price_US'],x)
result = reg_model.fit()
print(result.summary())
## Three years of emissions
result.tvalues.to_frame()
#Monthly Oil futures to future gasoline price
#Monthly
analysis = oil_co2_df.copy()
oil_co2_df.columns
analysis_month = analysis.groupby('Par_Month').last()[['future_oil_contract_1','Gasoline_Price_US','co2_conc']]
analysis_reg_month = np.log(analysis_month) - np.log(analysis_month.shift(1))
analysis_reg_month.dropna(inplace = True)
analysis_reg_month['co2_conc'] = analysis_reg_month['co2_conc'] - analysis_reg_month['co2_conc'].shift(12)
analysis_reg_month.dropna(inplace = True)
### Month ###
analysis_reg_month_edited = analysis_reg_month.copy()
analysis_reg_month_edited['positive_change'] = list(map(lambda x : 0 if x <= 0 else x,analysis_reg_month['future_oil_contract_1'] ))
analysis_reg_month_edited['negative_change'] = list(map(lambda x : 0 if x >= 0 else x,analysis_reg_month['future_oil_contract_1'] ))
analysis_reg_month_edited['p+_lag1'] = analysis_reg_month_edited['positive_change'].shift(1)
analysis_reg_month_edited['p+_lag2'] = analysis_reg_month_edited['positive_change'].shift(2)
analysis_reg_month_edited['p+_lag3'] = analysis_reg_month_edited['positive_change'].shift(3)
analysis_reg_month_edited['p+_lag4'] = analysis_reg_month_edited['positive_change'].shift(4)
analysis_reg_month_edited['p+_lag5'] = analysis_reg_month_edited['positive_change'].shift(5)
analysis_reg_month_edited['p+_lag6'] = analysis_reg_month_edited['positive_change'].shift(6)
analysis_reg_month_edited['p+_lag7'] = analysis_reg_month_edited['positive_change'].shift(7)
analysis_reg_month_edited['p+_lag8'] = analysis_reg_month_edited['positive_change'].shift(8)
analysis_reg_month_edited['p+_lag9'] = analysis_reg_month_edited['positive_change'].shift(9)
analysis_reg_month_edited['p+_lag10'] = analysis_reg_month_edited['positive_change'].shift(10)
analysis_reg_month_edited['chg_future_gasoline_lag-1'] = analysis_reg_month_edited['Gasoline_Price_US'].shift(-1)
analysis_reg_month_edited['p-_lag1'] = analysis_reg_month_edited['negative_change'].shift(1)
analysis_reg_month_edited['p-_lag2'] = analysis_reg_month_edited['negative_change'].shift(2)
analysis_reg_month_edited['p-_lag3'] = analysis_reg_month_edited['negative_change'].shift(3)
analysis_reg_month_edited['p-_lag4'] = analysis_reg_month_edited['negative_change'].shift(4)
analysis_reg_month_edited['p-_lag5'] = analysis_reg_month_edited['negative_change'].shift(5)
analysis_reg_month_edited['p-_lag6'] = analysis_reg_month_edited['negative_change'].shift(6)
analysis_reg_month_edited['p-_lag7'] = analysis_reg_month_edited['negative_change'].shift(7)
analysis_reg_month_edited['p-_lag8'] = analysis_reg_month_edited['negative_change'].shift(8)
analysis_reg_month_edited['p-_lag9'] = analysis_reg_month_edited['negative_change'].shift(9)
analysis_reg_month_edited['p-_lag10'] = analysis_reg_month_edited['negative_change'].shift(10)
analysis_reg_month_edited = analysis_reg_month_edited.dropna()
#Monthly oil futures to future gasoline
x = analysis_reg_month_edited[['positive_change', 'negative_change','p+_lag1', 'p+_lag2',
'p+_lag3', 'p+_lag4', 'p+_lag5', 'p+_lag6', 'p+_lag7', 'p+_lag8',
'p+_lag9', 'p+_lag10', 'p-_lag1', 'p-_lag2', 'p-_lag3', 'p-_lag4',
'p-_lag5', 'p-_lag6', 'p-_lag7', 'p-_lag8', 'p-_lag9', 'p-_lag10']]
reg_model = sm.OLS(analysis_reg_month_edited['chg_future_gasoline_lag-1'],x)
result = reg_model.fit()
print(result.summary())
## Three years of emissions
result.tvalues.to_frame()
#Monthly CO2
x = analysis_reg_month_edited[['positive_change', 'negative_change','p+_lag1', 'p+_lag2',
'p+_lag3', 'p+_lag4', 'p+_lag5', 'p+_lag6', 'p+_lag7', 'p+_lag8',
'p+_lag9', 'p+_lag10', 'p-_lag1', 'p-_lag2', 'p-_lag3', 'p-_lag4',
'p-_lag5', 'p-_lag6', 'p-_lag7', 'p-_lag8', 'p-_lag9', 'p-_lag10']]
reg_model = sm.OLS(analysis_reg_month_edited['co2_conc'],x)
result = reg_model.fit()
print(result.summary())
## Three years of emissions
result.tvalues.to_frame()
### Weekly CO2 ###
### Weekly Lag 0###
analysis = oil_co2_df.copy()
oil_co2_df.columns
analysis_month = analysis.groupby('Par_Month').last()[['oil_spot_WTI','Gasoline_Price_US','co2_conc']]
analysis_reg_month = np.log(analysis_month) - np.log(analysis_month.shift(1))
analysis_reg_month.dropna(inplace = True)
analysis_reg_month['co2_conc'] = analysis_reg_month['co2_conc'] - analysis_reg_month['co2_conc'].shift(12)
analysis_reg_month.dropna(inplace = True)
analysis_reg_month_edited = analysis_reg_month.copy()
analysis_reg_month_edited['chg_oil_spot_WTI_Positive'] = list(map(lambda x : 0 if x <= 0 else x,analysis_reg_month['oil_spot_WTI'] ))
analysis_reg_month_edited['chg_oil_spot_WTI_Negative'] = list(map(lambda x : 0 if x >= 0 else x,analysis_reg_month['oil_spot_WTI'] ))
analysis_reg = analysis_reg_month_edited.copy()
x = analysis_reg[['chg_oil_spot_WTI_Positive', 'chg_oil_spot_WTI_Negative']]
reg_model = sm.OLS(analysis_reg['Gasoline_Price_US'],x)
result = reg_model.fit()
print(result.summary())
### Weekly ###
### Weekly Lag 3###
analysis = oil_co2_df.copy()
oil_co2_df.columns
analysis_month = analysis.groupby('Par_Month').last()[['oil_spot_WTI','Gasoline_Price_US','co2_conc']]
analysis_reg_month = np.log(analysis_month) - np.log(analysis_month.shift(1))
analysis_reg_month.dropna(inplace = True)
analysis_reg_month['co2_conc'] = analysis_reg_month['co2_conc'] - analysis_reg_month['co2_conc'].shift(12)
analysis_reg_month.dropna(inplace = True)
analysis_reg_month_edited = analysis_reg_month.copy()
analysis_reg_month_edited['chg_oil_spot_WTI_Positive'] = list(map(lambda x : 0 if x <= 0 else x,analysis_reg_month['oil_spot_WTI'] ))
analysis_reg_month_edited['chg_oil_spot_WTI_Negative'] = list(map(lambda x : 0 if x >= 0 else x,analysis_reg_month['oil_spot_WTI'] ))
analysis_reg = analysis_reg_month_edited.copy()
analysis_reg['p+_lag1'] = analysis_reg_month_edited['chg_oil_spot_WTI_Positive'].shift(1)
analysis_reg['p+_lag2'] = analysis_reg_month_edited['chg_oil_spot_WTI_Positive'].shift(2)
analysis_reg['p+_lag3'] = analysis_reg_month_edited['chg_oil_spot_WTI_Positive'].shift(3)
analysis_reg['p-_lag1'] = analysis_reg_month_edited['chg_oil_spot_WTI_Negative'].shift(1)
analysis_reg['p-_lag2'] = analysis_reg_month_edited['chg_oil_spot_WTI_Negative'].shift(2)
analysis_reg['p-_lag3'] = analysis_reg_month_edited['chg_oil_spot_WTI_Negative'].shift(3)
analysis_reg = analysis_reg.dropna(subset = ['p+_lag1','p-_lag1','p+_lag2','p-_lag2','p+_lag3','p-_lag3'])
x = analysis_reg[['chg_oil_spot_WTI_Positive', 'chg_oil_spot_WTI_Negative','p+_lag1','p-_lag1','p+_lag2','p-_lag2','p+_lag3','p-_lag3']]
reg_model = sm.OLS(analysis_reg['Gasoline_Price_US'],x)
result = reg_model.fit()
print(result.summary())
### Weekly ###
### Weekly Lag 6###
analysis = oil_co2_df.copy()
oil_co2_df.columns
analysis_month = analysis.groupby('Par_Month').last()[['oil_spot_WTI','Gasoline_Price_US','co2_conc']]
analysis_reg_month = np.log(analysis_month) - np.log(analysis_month.shift(1))
analysis_reg_month.dropna(inplace = True)
analysis_reg_month['co2_conc'] = analysis_reg_month['co2_conc'] - analysis_reg_month['co2_conc'].shift(12)
analysis_reg_month.dropna(inplace = True)
analysis_reg_month_edited = analysis_reg_month.copy()
analysis_reg_month_edited['chg_oil_spot_WTI_Positive'] = list(map(lambda x : 0 if x <= 0 else x,analysis_reg_month['oil_spot_WTI'] ))
analysis_reg_month_edited['chg_oil_spot_WTI_Negative'] = list(map(lambda x : 0 if x >= 0 else x,analysis_reg_month['oil_spot_WTI'] ))
analysis_reg = analysis_reg_month_edited.copy()
analysis_reg['p+_lag1'] = analysis_reg_month_edited['chg_oil_spot_WTI_Positive'].shift(1)
analysis_reg['p+_lag2'] = analysis_reg_month_edited['chg_oil_spot_WTI_Positive'].shift(2)
analysis_reg['p+_lag3'] = analysis_reg_month_edited['chg_oil_spot_WTI_Positive'].shift(3)
analysis_reg['p+_lag4'] = analysis_reg_month_edited['chg_oil_spot_WTI_Positive'].shift(4)
analysis_reg['p+_lag5'] = analysis_reg_month_edited['chg_oil_spot_WTI_Positive'].shift(5)
analysis_reg['p+_lag6'] = analysis_reg_month_edited['chg_oil_spot_WTI_Positive'].shift(6)
analysis_reg['p-_lag1'] = analysis_reg_month_edited['chg_oil_spot_WTI_Negative'].shift(1)
analysis_reg['p-_lag2'] = analysis_reg_month_edited['chg_oil_spot_WTI_Negative'].shift(2)
analysis_reg['p-_lag3'] = analysis_reg_month_edited['chg_oil_spot_WTI_Negative'].shift(3)
analysis_reg['p-_lag4'] = analysis_reg_month_edited['chg_oil_spot_WTI_Negative'].shift(4)
analysis_reg['p-_lag5'] = analysis_reg_month_edited['chg_oil_spot_WTI_Negative'].shift(5)
analysis_reg['p-_lag6'] = analysis_reg_month_edited['chg_oil_spot_WTI_Negative'].shift(6)
analysis_reg = analysis_reg.dropna(subset = ['p+_lag1','p-_lag1','p+_lag2','p-_lag2','p+_lag3','p-_lag3','p+_lag4','p-_lag4','p+_lag5','p-_lag5','p+_lag6','p-_lag6'])
x = analysis_reg[['chg_oil_spot_WTI_Positive', 'chg_oil_spot_WTI_Negative','p+_lag1','p-_lag1','p+_lag2','p-_lag2','p+_lag3','p-_lag3','p+_lag4','p-_lag4','p+_lag5','p-_lag5','p+_lag6','p-_lag6']]
reg_model = sm.OLS(analysis_reg['Gasoline_Price_US'],x)
result = reg_model.fit()
print(result.summary())
### Weekly ###
### Weekly Lag 12###
analysis = oil_co2_df.copy()
oil_co2_df.columns
analysis_month = analysis.groupby('Par_Month').last()[['oil_spot_WTI','Gasoline_Price_US','co2_conc']]
analysis_reg_month = np.log(analysis_month) - np.log(analysis_month.shift(1))
analysis_reg_month.dropna(inplace = True)
analysis_reg_month['co2_conc'] = analysis_reg_month['co2_conc'] - analysis_reg_month['co2_conc'].shift(12)
analysis_reg_month.dropna(inplace = True)
analysis_reg_month_edited = analysis_reg_month.copy()
analysis_reg_month_edited['chg_oil_spot_WTI_Positive'] = list(map(lambda x : 0 if x <= 0 else x,analysis_reg_month['oil_spot_WTI'] ))
analysis_reg_month_edited['chg_oil_spot_WTI_Negative'] = list(map(lambda x : 0 if x >= 0 else x,analysis_reg_month['oil_spot_WTI'] ))
analysis_reg = analysis_reg_month_edited.copy()
analysis_reg['p+_lag1'] = analysis_reg_month_edited['chg_oil_spot_WTI_Positive'].shift(1)
analysis_reg['p+_lag2'] = analysis_reg_month_edited['chg_oil_spot_WTI_Positive'].shift(2)
analysis_reg['p+_lag3'] = analysis_reg_month_edited['chg_oil_spot_WTI_Positive'].shift(3)
analysis_reg['p+_lag4'] = analysis_reg_month_edited['chg_oil_spot_WTI_Positive'].shift(4)
analysis_reg['p+_lag5'] = analysis_reg_month_edited['chg_oil_spot_WTI_Positive'].shift(5)
analysis_reg['p+_lag6'] = analysis_reg_month_edited['chg_oil_spot_WTI_Positive'].shift(6)
analysis_reg['p+_lag7'] = analysis_reg_month_edited['chg_oil_spot_WTI_Positive'].shift(7)
analysis_reg['p+_lag8'] = analysis_reg_month_edited['chg_oil_spot_WTI_Positive'].shift(8)
analysis_reg['p+_lag9'] = analysis_reg_month_edited['chg_oil_spot_WTI_Positive'].shift(9)
analysis_reg['p+_lag10'] = analysis_reg_month_edited['chg_oil_spot_WTI_Positive'].shift(10)
analysis_reg['p+_lag11'] = analysis_reg_month_edited['chg_oil_spot_WTI_Positive'].shift(11)
analysis_reg['p+_lag12'] = analysis_reg_month_edited['chg_oil_spot_WTI_Positive'].shift(12)
analysis_reg['p-_lag1'] = analysis_reg_month_edited['chg_oil_spot_WTI_Negative'].shift(1)
analysis_reg['p-_lag2'] = analysis_reg_month_edited['chg_oil_spot_WTI_Negative'].shift(2)
analysis_reg['p-_lag3'] = analysis_reg_month_edited['chg_oil_spot_WTI_Negative'].shift(3)
analysis_reg['p-_lag4'] = analysis_reg_month_edited['chg_oil_spot_WTI_Negative'].shift(4)
analysis_reg['p-_lag5'] = analysis_reg_month_edited['chg_oil_spot_WTI_Negative'].shift(5)
analysis_reg['p-_lag6'] = analysis_reg_month_edited['chg_oil_spot_WTI_Negative'].shift(6)
analysis_reg['p-_lag7'] = analysis_reg_month_edited['chg_oil_spot_WTI_Negative'].shift(7)
analysis_reg['p-_lag8'] = analysis_reg_month_edited['chg_oil_spot_WTI_Negative'].shift(8)
analysis_reg['p-_lag9'] = analysis_reg_month_edited['chg_oil_spot_WTI_Negative'].shift(9)
analysis_reg['p-_lag10'] = analysis_reg_month_edited['chg_oil_spot_WTI_Negative'].shift(10)
analysis_reg['p-_lag11'] = analysis_reg_month_edited['chg_oil_spot_WTI_Negative'].shift(11)
analysis_reg['p-_lag12'] = analysis_reg_month_edited['chg_oil_spot_WTI_Negative'].shift(12)
analysis_reg = analysis_reg.dropna(subset = ['p+_lag1','p-_lag1','p+_lag2','p-_lag2','p+_lag3','p-_lag3','p+_lag4','p-_lag4','p+_lag5','p-_lag5','p+_lag6','p-_lag6','p+_lag7','p-_lag7','p+_lag8','p-_lag8','p+_lag9','p-_lag9','p+_lag10','p-_lag10','p+_lag11','p-_lag11','p+_lag12','p-_lag12'])
x = analysis_reg[['chg_oil_spot_WTI_Positive', 'chg_oil_spot_WTI_Negative','p+_lag1','p-_lag1','p+_lag2','p-_lag2','p+_lag3','p-_lag3','p+_lag4','p-_lag4','p+_lag5','p-_lag5','p+_lag6','p-_lag6','p+_lag7','p-_lag7','p+_lag8','p-_lag8','p+_lag9','p-_lag9','p+_lag10','p-_lag10','p+_lag11','p-_lag11','p+_lag12','p-_lag12']]
reg_model = sm.OLS(analysis_reg['Gasoline_Price_US'],x)
result = reg_model.fit()
print(result.summary())
### Weekly ###
### Weekly Lag 24###
analysis = oil_co2_df.copy()
oil_co2_df.columns
analysis_month = analysis.groupby('Par_Month').last()[['oil_spot_WTI','Gasoline_Price_US','co2_conc']]
analysis_reg_month = np.log(analysis_month) - np.log(analysis_month.shift(1))
analysis_reg_month.dropna(inplace = True)
analysis_reg_month['co2_conc'] = analysis_reg_month['co2_conc'] - analysis_reg_month['co2_conc'].shift(12)
analysis_reg_month.dropna(inplace = True)
analysis_reg_month_edited = analysis_reg_month.copy()
analysis_reg_month_edited['chg_oil_spot_WTI_Positive'] = list(map(lambda x : 0 if x <= 0 else x,analysis_reg_month['oil_spot_WTI'] ))
analysis_reg_month_edited['chg_oil_spot_WTI_Negative'] = list(map(lambda x : 0 if x >= 0 else x,analysis_reg_month['oil_spot_WTI'] ))
analysis_reg = analysis_reg_month_edited.copy()
analysis_reg_month_edited['chg_oil_spot_WTI_Positive'] = list(map(lambda x : 0 if x <= 0 else x,analysis_reg_month['oil_spot_WTI'] ))
analysis_reg_month_edited['chg_oil_spot_WTI_Negative'] = list(map(lambda x : 0 if x >= 0 else x,analysis_reg_month['oil_spot_WTI'] ))
analysis_reg = analysis_reg_month_edited.copy()
analysis_reg['p+_lag1'] = analysis_reg_month_edited['chg_oil_spot_WTI_Positive'].shift(1)
analysis_reg['p+_lag2'] = analysis_reg_month_edited['chg_oil_spot_WTI_Positive'].shift(2)
analysis_reg['p+_lag3'] = analysis_reg_month_edited['chg_oil_spot_WTI_Positive'].shift(3)
analysis_reg['p+_lag4'] = analysis_reg_month_edited['chg_oil_spot_WTI_Positive'].shift(4)
analysis_reg['p+_lag5'] = analysis_reg_month_edited['chg_oil_spot_WTI_Positive'].shift(5)
analysis_reg['p+_lag6'] = analysis_reg_month_edited['chg_oil_spot_WTI_Positive'].shift(6)
analysis_reg['p+_lag7'] = analysis_reg_month_edited['chg_oil_spot_WTI_Positive'].shift(7)
analysis_reg['p+_lag8'] = analysis_reg_month_edited['chg_oil_spot_WTI_Positive'].shift(8)
analysis_reg['p+_lag9'] = analysis_reg_month_edited['chg_oil_spot_WTI_Positive'].shift(9)
analysis_reg['p+_lag10'] = analysis_reg_month_edited['chg_oil_spot_WTI_Positive'].shift(10)
analysis_reg['p+_lag11'] = analysis_reg_month_edited['chg_oil_spot_WTI_Positive'].shift(11)
analysis_reg['p+_lag12'] = analysis_reg_month_edited['chg_oil_spot_WTI_Positive'].shift(12)
analysis_reg['p+_lag13'] = analysis_reg_month_edited['chg_oil_spot_WTI_Positive'].shift(13)
analysis_reg['p+_lag14'] = analysis_reg_month_edited['chg_oil_spot_WTI_Positive'].shift(14)
analysis_reg['p+_lag15'] = analysis_reg_month_edited['chg_oil_spot_WTI_Positive'].shift(15)
analysis_reg['p+_lag16'] = analysis_reg_month_edited['chg_oil_spot_WTI_Positive'].shift(16)
analysis_reg['p+_lag17'] = analysis_reg_month_edited['chg_oil_spot_WTI_Positive'].shift(17)
analysis_reg['p+_lag18'] = analysis_reg_month_edited['chg_oil_spot_WTI_Positive'].shift(18)
analysis_reg['p+_lag19'] = analysis_reg_month_edited['chg_oil_spot_WTI_Positive'].shift(19)
analysis_reg['p+_lag20'] = analysis_reg_month_edited['chg_oil_spot_WTI_Positive'].shift(20)
analysis_reg['p+_lag21'] = analysis_reg_month_edited['chg_oil_spot_WTI_Positive'].shift(21)
analysis_reg['p+_lag22'] = analysis_reg_month_edited['chg_oil_spot_WTI_Positive'].shift(22)
analysis_reg['p+_lag23'] = analysis_reg_month_edited['chg_oil_spot_WTI_Positive'].shift(23)
analysis_reg['p+_lag24'] = analysis_reg_month_edited['chg_oil_spot_WTI_Positive'].shift(24)
analysis_reg['p-_lag1'] = analysis_reg_month_edited['chg_oil_spot_WTI_Negative'].shift(1)
analysis_reg['p-_lag2'] = analysis_reg_month_edited['chg_oil_spot_WTI_Negative'].shift(2)
analysis_reg['p-_lag3'] = analysis_reg_month_edited['chg_oil_spot_WTI_Negative'].shift(3)
analysis_reg['p-_lag4'] = analysis_reg_month_edited['chg_oil_spot_WTI_Negative'].shift(4)
analysis_reg['p-_lag5'] = analysis_reg_month_edited['chg_oil_spot_WTI_Negative'].shift(5)
analysis_reg['p-_lag6'] = analysis_reg_month_edited['chg_oil_spot_WTI_Negative'].shift(6)
analysis_reg['p-_lag7'] = analysis_reg_month_edited['chg_oil_spot_WTI_Negative'].shift(7)
analysis_reg['p-_lag8'] = analysis_reg_month_edited['chg_oil_spot_WTI_Negative'].shift(8)
analysis_reg['p-_lag9'] = analysis_reg_month_edited['chg_oil_spot_WTI_Negative'].shift(9)
analysis_reg['p-_lag10'] = analysis_reg_month_edited['chg_oil_spot_WTI_Negative'].shift(10)
analysis_reg['p-_lag11'] = analysis_reg_month_edited['chg_oil_spot_WTI_Negative'].shift(11)
analysis_reg['p-_lag12'] = analysis_reg_month_edited['chg_oil_spot_WTI_Negative'].shift(12)
analysis_reg['p-_lag13'] = analysis_reg_month_edited['chg_oil_spot_WTI_Negative'].shift(13)
analysis_reg['p-_lag14'] = analysis_reg_month_edited['chg_oil_spot_WTI_Negative'].shift(14)
analysis_reg['p-_lag15'] = analysis_reg_month_edited['chg_oil_spot_WTI_Negative'].shift(15)
analysis_reg['p-_lag16'] = analysis_reg_month_edited['chg_oil_spot_WTI_Negative'].shift(16)
analysis_reg['p-_lag17'] = analysis_reg_month_edited['chg_oil_spot_WTI_Negative'].shift(17)
analysis_reg['p-_lag18'] = analysis_reg_month_edited['chg_oil_spot_WTI_Negative'].shift(18)
analysis_reg['p-_lag19'] = analysis_reg_month_edited['chg_oil_spot_WTI_Negative'].shift(19)
analysis_reg['p-_lag20'] = analysis_reg_month_edited['chg_oil_spot_WTI_Negative'].shift(20)
analysis_reg['p-_lag21'] = analysis_reg_month_edited['chg_oil_spot_WTI_Negative'].shift(21)
analysis_reg['p-_lag22'] = analysis_reg_month_edited['chg_oil_spot_WTI_Negative'].shift(22)
analysis_reg['p-_lag23'] = analysis_reg_month_edited['chg_oil_spot_WTI_Negative'].shift(23)
analysis_reg['p-_lag24'] = analysis_reg_month_edited['chg_oil_spot_WTI_Negative'].shift(24)
analysis_reg = analysis_reg.dropna(subset = ['p+_lag1','p-_lag1','p+_lag2','p-_lag2','p+_lag3','p-_lag3','p+_lag4','p-_lag4','p+_lag5','p-_lag5','p+_lag6','p-_lag6','p+_lag7','p-_lag7','p+_lag8','p-_lag8','p+_lag9','p-_lag9','p+_lag10','p-_lag10','p+_lag11','p-_lag11','p+_lag12','p-_lag12'
,'p+_lag13','p-_lag13','p+_lag14','p-_lag14','p+_lag15','p-_lag15','p+_lag16','p-_lag16','p+_lag17','p-_lag17','p+_lag18','p-_lag18','p+_lag19','p-_lag19','p+_lag20','p-_lag20','p+_lag21','p-_lag21','p+_lag22','p-_lag22','p+_lag23','p-_lag23','p+_lag24','p-_lag24'])
x = analysis_reg[['chg_oil_spot_WTI_Positive', 'chg_oil_spot_WTI_Negative','p+_lag1','p-_lag1','p+_lag2','p-_lag2','p+_lag3','p-_lag3','p+_lag4','p-_lag4','p+_lag5','p-_lag5','p+_lag6','p-_lag6','p+_lag7','p-_lag7','p+_lag8','p-_lag8','p+_lag9','p-_lag9','p+_lag10','p-_lag10','p+_lag11','p-_lag11','p+_lag12','p-_lag12'
,'p+_lag13','p-_lag13','p+_lag14','p-_lag14','p+_lag15','p-_lag15','p+_lag16','p-_lag16','p+_lag17','p-_lag17','p+_lag18','p-_lag18','p+_lag19','p-_lag19','p+_lag20','p-_lag20','p+_lag21','p-_lag21','p+_lag22','p-_lag22','p+_lag23','p-_lag23','p+_lag24','p-_lag24']]
reg_model = sm.OLS(analysis_reg['Gasoline_Price_US'],x)
result = reg_model.fit()
print(result.summary())
### Monthly CO2 ###
### Monthly Lag 0###
analysis = oil_co2_df.copy()
oil_co2_df.columns
analysis_month = analysis.groupby('Par_Month').last()[['oil_spot_WTI','Gasoline_Price_US','co2_conc']]
analysis_reg_month = np.log(analysis_month) - np.log(analysis_month.shift(1))
analysis_reg_month.dropna(inplace = True)
analysis_reg_month['co2_conc'] = analysis_reg_month['co2_conc'] - analysis_reg_month['co2_conc'].shift(12)
analysis_reg_month.dropna(inplace = True)
analysis_reg_month_edited = analysis_reg_month.copy()
analysis_reg_month_edited['chg_oil_spot_WTI_Positive'] = list(map(lambda x : 0 if x <= 0 else x,analysis_reg_month['oil_spot_WTI'] ))
analysis_reg_month_edited['chg_oil_spot_WTI_Negative'] = list(map(lambda x : 0 if x >= 0 else x,analysis_reg_month['oil_spot_WTI'] ))
analysis_reg = analysis_reg_month_edited.copy()
x = analysis_reg[['chg_oil_spot_WTI_Positive', 'chg_oil_spot_WTI_Negative']]
reg_model = sm.OLS(analysis_reg['co2_conc'],x)
result = reg_model.fit()
print(result.summary())
### Monthly ###
### Monthly Lag 3###
analysis = oil_co2_df.copy()
oil_co2_df.columns
analysis_month = analysis.groupby('Par_Month').last()[['oil_spot_WTI','Gasoline_Price_US','co2_conc']]
analysis_reg_month = np.log(analysis_month) - np.log(analysis_month.shift(1))
analysis_reg_month.dropna(inplace = True)
analysis_reg_month['co2_conc'] = analysis_reg_month['co2_conc'] - analysis_reg_month['co2_conc'].shift(12)
analysis_reg_month.dropna(inplace = True)
analysis_reg_month_edited = analysis_reg_month.copy()
analysis_reg_month_edited['chg_oil_spot_WTI_Positive'] = list(map(lambda x : 0 if x <= 0 else x,analysis_reg_month['oil_spot_WTI'] ))
analysis_reg_month_edited['chg_oil_spot_WTI_Negative'] = list(map(lambda x : 0 if x >= 0 else x,analysis_reg_month['oil_spot_WTI'] ))
analysis_reg = analysis_reg_month_edited.copy()
analysis_reg['p+_lag1'] = analysis_reg_month_edited['chg_oil_spot_WTI_Positive'].shift(1)
analysis_reg['p+_lag2'] = analysis_reg_month_edited['chg_oil_spot_WTI_Positive'].shift(2)
analysis_reg['p+_lag3'] = analysis_reg_month_edited['chg_oil_spot_WTI_Positive'].shift(3)
analysis_reg['p-_lag1'] = analysis_reg_month_edited['chg_oil_spot_WTI_Negative'].shift(1)
analysis_reg['p-_lag2'] = analysis_reg_month_edited['chg_oil_spot_WTI_Negative'].shift(2)
analysis_reg['p-_lag3'] = analysis_reg_month_edited['chg_oil_spot_WTI_Negative'].shift(3)
analysis_reg = analysis_reg.dropna(subset = ['p+_lag1','p-_lag1','p+_lag2','p-_lag2','p+_lag3','p-_lag3'])
x = analysis_reg[['chg_oil_spot_WTI_Positive', 'chg_oil_spot_WTI_Negative','p+_lag1','p-_lag1','p+_lag2','p-_lag2','p+_lag3','p-_lag3']]
reg_model = sm.OLS(analysis_reg['co2_conc'],x)
result = reg_model.fit()
print(result.summary())
### Monthly ###
### Monthly Lag 6###
analysis = oil_co2_df.copy()
oil_co2_df.columns
analysis_month = analysis.groupby('Par_Month').last()[['oil_spot_WTI','Gasoline_Price_US','co2_conc']]
analysis_reg_month = np.log(analysis_month) - np.log(analysis_month.shift(1))
analysis_reg_month.dropna(inplace = True)
analysis_reg_month['co2_conc'] = analysis_reg_month['co2_conc'] - analysis_reg_month['co2_conc'].shift(12)
analysis_reg_month.dropna(inplace = True)
analysis_reg_month_edited = analysis_reg_month.copy()
analysis_reg_month_edited['chg_oil_spot_WTI_Positive'] = list(map(lambda x : 0 if x <= 0 else x,analysis_reg_month['oil_spot_WTI'] ))
analysis_reg_month_edited['chg_oil_spot_WTI_Negative'] = list(map(lambda x : 0 if x >= 0 else x,analysis_reg_month['oil_spot_WTI'] ))
analysis_reg = analysis_reg_month_edited.copy()
analysis_reg['p+_lag1'] = analysis_reg_month_edited['chg_oil_spot_WTI_Positive'].shift(1)
analysis_reg['p+_lag2'] = analysis_reg_month_edited['chg_oil_spot_WTI_Positive'].shift(2)
analysis_reg['p+_lag3'] = analysis_reg_month_edited['chg_oil_spot_WTI_Positive'].shift(3)
analysis_reg['p+_lag4'] = analysis_reg_month_edited['chg_oil_spot_WTI_Positive'].shift(4)
analysis_reg['p+_lag5'] = analysis_reg_month_edited['chg_oil_spot_WTI_Positive'].shift(5)
analysis_reg['p+_lag6'] = analysis_reg_month_edited['chg_oil_spot_WTI_Positive'].shift(6)
analysis_reg['p-_lag1'] = analysis_reg_month_edited['chg_oil_spot_WTI_Negative'].shift(1)
analysis_reg['p-_lag2'] = analysis_reg_month_edited['chg_oil_spot_WTI_Negative'].shift(2)
analysis_reg['p-_lag3'] = analysis_reg_month_edited['chg_oil_spot_WTI_Negative'].shift(3)
analysis_reg['p-_lag4'] = analysis_reg_month_edited['chg_oil_spot_WTI_Negative'].shift(4)
analysis_reg['p-_lag5'] = analysis_reg_month_edited['chg_oil_spot_WTI_Negative'].shift(5)
analysis_reg['p-_lag6'] = analysis_reg_month_edited['chg_oil_spot_WTI_Negative'].shift(6)
analysis_reg = analysis_reg.dropna(subset = ['p+_lag1','p-_lag1','p+_lag2','p-_lag2','p+_lag3','p-_lag3','p+_lag4','p-_lag4','p+_lag5','p-_lag5','p+_lag6','p-_lag6'])
x = analysis_reg[['chg_oil_spot_WTI_Positive', 'chg_oil_spot_WTI_Negative','p+_lag1','p-_lag1','p+_lag2','p-_lag2','p+_lag3','p-_lag3','p+_lag4','p-_lag4','p+_lag5','p-_lag5','p+_lag6','p-_lag6']]
reg_model = sm.OLS(analysis_reg['co2_conc'],x)
result = reg_model.fit()
print(result.summary())
### Monthly ###
### Monthly Lag 12###
analysis = oil_co2_df.copy()
oil_co2_df.columns
analysis_month = analysis.groupby('Par_Month').last()[['oil_spot_WTI','Gasoline_Price_US','co2_conc']]
analysis_reg_month = np.log(analysis_month) - np.log(analysis_month.shift(1))
analysis_reg_month.dropna(inplace = True)
analysis_reg_month['co2_conc'] = analysis_reg_month['co2_conc'] - analysis_reg_month['co2_conc'].shift(12)
analysis_reg_month.dropna(inplace = True)
analysis_reg_month_edited = analysis_reg_month.copy()
analysis_reg_month_edited['chg_oil_spot_WTI_Positive'] = list(map(lambda x : 0 if x <= 0 else x,analysis_reg_month['oil_spot_WTI'] ))
analysis_reg_month_edited['chg_oil_spot_WTI_Negative'] = list(map(lambda x : 0 if x >= 0 else x,analysis_reg_month['oil_spot_WTI'] ))
analysis_reg = analysis_reg_month_edited.copy()
analysis_reg['p+_lag1'] = analysis_reg_month_edited['chg_oil_spot_WTI_Positive'].shift(1)
analysis_reg['p+_lag2'] = analysis_reg_month_edited['chg_oil_spot_WTI_Positive'].shift(2)
analysis_reg['p+_lag3'] = analysis_reg_month_edited['chg_oil_spot_WTI_Positive'].shift(3)
analysis_reg['p+_lag4'] = analysis_reg_month_edited['chg_oil_spot_WTI_Positive'].shift(4)
analysis_reg['p+_lag5'] = analysis_reg_month_edited['chg_oil_spot_WTI_Positive'].shift(5)
analysis_reg['p+_lag6'] = analysis_reg_month_edited['chg_oil_spot_WTI_Positive'].shift(6)
analysis_reg['p+_lag7'] = analysis_reg_month_edited['chg_oil_spot_WTI_Positive'].shift(7)
analysis_reg['p+_lag8'] = analysis_reg_month_edited['chg_oil_spot_WTI_Positive'].shift(8)
analysis_reg['p+_lag9'] = analysis_reg_month_edited['chg_oil_spot_WTI_Positive'].shift(9)
analysis_reg['p+_lag10'] = analysis_reg_month_edited['chg_oil_spot_WTI_Positive'].shift(10)
analysis_reg['p+_lag11'] = analysis_reg_month_edited['chg_oil_spot_WTI_Positive'].shift(11)
analysis_reg['p+_lag12'] = analysis_reg_month_edited['chg_oil_spot_WTI_Positive'].shift(12)
analysis_reg['p-_lag1'] = analysis_reg_month_edited['chg_oil_spot_WTI_Negative'].shift(1)
analysis_reg['p-_lag2'] = analysis_reg_month_edited['chg_oil_spot_WTI_Negative'].shift(2)
analysis_reg['p-_lag3'] = analysis_reg_month_edited['chg_oil_spot_WTI_Negative'].shift(3)
analysis_reg['p-_lag4'] = analysis_reg_month_edited['chg_oil_spot_WTI_Negative'].shift(4)
analysis_reg['p-_lag5'] = analysis_reg_month_edited['chg_oil_spot_WTI_Negative'].shift(5)
analysis_reg['p-_lag6'] = analysis_reg_month_edited['chg_oil_spot_WTI_Negative'].shift(6)
analysis_reg['p-_lag7'] = analysis_reg_month_edited['chg_oil_spot_WTI_Negative'].shift(7)
analysis_reg['p-_lag8'] = analysis_reg_month_edited['chg_oil_spot_WTI_Negative'].shift(8)
analysis_reg['p-_lag9'] = analysis_reg_month_edited['chg_oil_spot_WTI_Negative'].shift(9)
analysis_reg['p-_lag10'] = analysis_reg_month_edited['chg_oil_spot_WTI_Negative'].shift(10)
analysis_reg['p-_lag11'] = analysis_reg_month_edited['chg_oil_spot_WTI_Negative'].shift(11)
analysis_reg['p-_lag12'] = analysis_reg_month_edited['chg_oil_spot_WTI_Negative'].shift(12)
analysis_reg = analysis_reg.dropna(subset = ['p+_lag1','p-_lag1','p+_lag2','p-_lag2','p+_lag3','p-_lag3','p+_lag4','p-_lag4','p+_lag5','p-_lag5','p+_lag6','p-_lag6','p+_lag7','p-_lag7','p+_lag8','p-_lag8','p+_lag9','p-_lag9','p+_lag10','p-_lag10','p+_lag11','p-_lag11','p+_lag12','p-_lag12'])
x = analysis_reg[['chg_oil_spot_WTI_Positive', 'chg_oil_spot_WTI_Negative','p+_lag1','p-_lag1','p+_lag2','p-_lag2','p+_lag3','p-_lag3','p+_lag4','p-_lag4','p+_lag5','p-_lag5','p+_lag6','p-_lag6','p+_lag7','p-_lag7','p+_lag8','p-_lag8','p+_lag9','p-_lag9','p+_lag10','p-_lag10','p+_lag11','p-_lag11','p+_lag12','p-_lag12']]
reg_model = sm.OLS(analysis_reg['co2_conc'],x)
result = reg_model.fit()
print(result.summary())
### Monthly ###
### Monthly Lag 24###
analysis = oil_co2_df.copy()
oil_co2_df.columns
analysis_month = analysis.groupby('Par_Month').last()[['oil_spot_WTI','Gasoline_Price_US','co2_conc']]
analysis_reg_month = np.log(analysis_month) - np.log(analysis_month.shift(1))
analysis_reg_month.dropna(inplace = True)
analysis_reg_month['co2_conc'] = analysis_reg_month['co2_conc'] - analysis_reg_month['co2_conc'].shift(12)
analysis_reg_month.dropna(inplace = True)
analysis_reg_month_edited = analysis_reg_month.copy()
analysis_reg_month_edited['chg_oil_spot_WTI_Positive'] = list(map(lambda x : 0 if x <= 0 else x,analysis_reg_month['oil_spot_WTI'] ))
analysis_reg_month_edited['chg_oil_spot_WTI_Negative'] = list(map(lambda x : 0 if x >= 0 else x,analysis_reg_month['oil_spot_WTI'] ))
analysis_reg = analysis_reg_month_edited.copy()
analysis_reg = analysis_reg_month_edited.copy()
analysis_reg['p+_lag1'] = analysis_reg_month_edited['chg_oil_spot_WTI_Positive'].shift(1)
analysis_reg['p+_lag2'] = analysis_reg_month_edited['chg_oil_spot_WTI_Positive'].shift(2)
analysis_reg['p+_lag3'] = analysis_reg_month_edited['chg_oil_spot_WTI_Positive'].shift(3)
analysis_reg['p+_lag4'] = analysis_reg_month_edited['chg_oil_spot_WTI_Positive'].shift(4)
analysis_reg['p+_lag5'] = analysis_reg_month_edited['chg_oil_spot_WTI_Positive'].shift(5)
analysis_reg['p+_lag6'] = analysis_reg_month_edited['chg_oil_spot_WTI_Positive'].shift(6)
analysis_reg['p+_lag7'] = analysis_reg_month_edited['chg_oil_spot_WTI_Positive'].shift(7)
analysis_reg['p+_lag8'] = analysis_reg_month_edited['chg_oil_spot_WTI_Positive'].shift(8)
analysis_reg['p+_lag9'] = analysis_reg_month_edited['chg_oil_spot_WTI_Positive'].shift(9)
analysis_reg['p+_lag10'] = analysis_reg_month_edited['chg_oil_spot_WTI_Positive'].shift(10)
analysis_reg['p+_lag11'] = analysis_reg_month_edited['chg_oil_spot_WTI_Positive'].shift(11)
analysis_reg['p+_lag12'] = analysis_reg_month_edited['chg_oil_spot_WTI_Positive'].shift(12)
analysis_reg['p+_lag13'] = analysis_reg_month_edited['chg_oil_spot_WTI_Positive'].shift(13)
analysis_reg['p+_lag14'] = analysis_reg_month_edited['chg_oil_spot_WTI_Positive'].shift(14)
analysis_reg['p+_lag15'] = analysis_reg_month_edited['chg_oil_spot_WTI_Positive'].shift(15)
analysis_reg['p+_lag16'] = analysis_reg_month_edited['chg_oil_spot_WTI_Positive'].shift(16)
analysis_reg['p+_lag17'] = analysis_reg_month_edited['chg_oil_spot_WTI_Positive'].shift(17)
analysis_reg['p+_lag18'] = analysis_reg_month_edited['chg_oil_spot_WTI_Positive'].shift(18)
analysis_reg['p+_lag19'] = analysis_reg_month_edited['chg_oil_spot_WTI_Positive'].shift(19)
analysis_reg['p+_lag20'] = analysis_reg_month_edited['chg_oil_spot_WTI_Positive'].shift(20)
analysis_reg['p+_lag21'] = analysis_reg_month_edited['chg_oil_spot_WTI_Positive'].shift(21)
analysis_reg['p+_lag22'] = analysis_reg_month_edited['chg_oil_spot_WTI_Positive'].shift(22)
analysis_reg['p+_lag23'] = analysis_reg_month_edited['chg_oil_spot_WTI_Positive'].shift(23)
analysis_reg['p+_lag24'] = analysis_reg_month_edited['chg_oil_spot_WTI_Positive'].shift(24)
analysis_reg['p-_lag1'] = analysis_reg_month_edited['chg_oil_spot_WTI_Negative'].shift(1)
analysis_reg['p-_lag2'] = analysis_reg_month_edited['chg_oil_spot_WTI_Negative'].shift(2)
analysis_reg['p-_lag3'] = analysis_reg_month_edited['chg_oil_spot_WTI_Negative'].shift(3)
analysis_reg['p-_lag4'] = analysis_reg_month_edited['chg_oil_spot_WTI_Negative'].shift(4)
analysis_reg['p-_lag5'] = analysis_reg_month_edited['chg_oil_spot_WTI_Negative'].shift(5)
analysis_reg['p-_lag6'] = analysis_reg_month_edited['chg_oil_spot_WTI_Negative'].shift(6)
analysis_reg['p-_lag7'] = analysis_reg_month_edited['chg_oil_spot_WTI_Negative'].shift(7)
analysis_reg['p-_lag8'] = analysis_reg_month_edited['chg_oil_spot_WTI_Negative'].shift(8)
analysis_reg['p-_lag9'] = analysis_reg_month_edited['chg_oil_spot_WTI_Negative'].shift(9)
analysis_reg['p-_lag10'] = analysis_reg_month_edited['chg_oil_spot_WTI_Negative'].shift(10)
analysis_reg['p-_lag11'] = analysis_reg_month_edited['chg_oil_spot_WTI_Negative'].shift(11)
analysis_reg['p-_lag12'] = analysis_reg_month_edited['chg_oil_spot_WTI_Negative'].shift(12)
analysis_reg['p-_lag13'] = analysis_reg_month_edited['chg_oil_spot_WTI_Negative'].shift(13)
analysis_reg['p-_lag14'] = analysis_reg_month_edited['chg_oil_spot_WTI_Negative'].shift(14)
analysis_reg['p-_lag15'] = analysis_reg_month_edited['chg_oil_spot_WTI_Negative'].shift(15)
analysis_reg['p-_lag16'] = analysis_reg_month_edited['chg_oil_spot_WTI_Negative'].shift(16)
analysis_reg['p-_lag17'] = analysis_reg_month_edited['chg_oil_spot_WTI_Negative'].shift(17)
analysis_reg['p-_lag18'] = analysis_reg_month_edited['chg_oil_spot_WTI_Negative'].shift(18)
analysis_reg['p-_lag19'] = analysis_reg_month_edited['chg_oil_spot_WTI_Negative'].shift(19)
analysis_reg['p-_lag20'] = analysis_reg_month_edited['chg_oil_spot_WTI_Negative'].shift(20)
analysis_reg['p-_lag21'] = analysis_reg_month_edited['chg_oil_spot_WTI_Negative'].shift(21)
analysis_reg['p-_lag22'] = analysis_reg_month_edited['chg_oil_spot_WTI_Negative'].shift(22)
analysis_reg['p-_lag23'] = analysis_reg_month_edited['chg_oil_spot_WTI_Negative'].shift(23)
analysis_reg['p-_lag24'] = analysis_reg_month_edited['chg_oil_spot_WTI_Negative'].shift(24)
analysis_reg = analysis_reg.dropna(subset = ['p+_lag1','p-_lag1','p+_lag2','p-_lag2','p+_lag3','p-_lag3','p+_lag4','p-_lag4','p+_lag5','p-_lag5','p+_lag6','p-_lag6','p+_lag7','p-_lag7','p+_lag8','p-_lag8','p+_lag9','p-_lag9','p+_lag10','p-_lag10','p+_lag11','p-_lag11','p+_lag12','p-_lag12'
,'p+_lag13','p-_lag13','p+_lag14','p-_lag14','p+_lag15','p-_lag15','p+_lag16','p-_lag16','p+_lag17','p-_lag17','p+_lag18','p-_lag18','p+_lag19','p-_lag19','p+_lag20','p-_lag20','p+_lag21','p-_lag21','p+_lag22','p-_lag22','p+_lag23','p-_lag23','p+_lag24','p-_lag24'])
x = analysis_reg[['chg_oil_spot_WTI_Positive', 'chg_oil_spot_WTI_Negative','p+_lag1','p-_lag1','p+_lag2','p-_lag2','p+_lag3','p-_lag3','p+_lag4','p-_lag4','p+_lag5','p-_lag5','p+_lag6','p-_lag6','p+_lag7','p-_lag7','p+_lag8','p-_lag8','p+_lag9','p-_lag9','p+_lag10','p-_lag10','p+_lag11','p-_lag11','p+_lag12','p-_lag12'
,'p+_lag13','p-_lag13','p+_lag14','p-_lag14','p+_lag15','p-_lag15','p+_lag16','p-_lag16','p+_lag17','p-_lag17','p+_lag18','p-_lag18','p+_lag19','p-_lag19','p+_lag20','p-_lag20','p+_lag21','p-_lag21','p+_lag22','p-_lag22','p+_lag23','p-_lag23','p+_lag24','p-_lag24']]
reg_model = sm.OLS(analysis_reg['co2_conc'],x)
result = reg_model.fit()
print(result.summary())
#Quarterly
analysis = oil_co2_df.copy()
oil_co2_df.columns
analysis_quarter = analysis.query('Month in [12,3,6,9]').groupby('Par_Month').last()[['oil_spot_WTI','Gasoline_Price_US','co2_conc']]
analysis_quarter
analysis_reg_quarter = np.log(analysis_quarter) - np.log(analysis_quarter.shift(1))
analysis_reg_quarter.dropna(inplace = True)
analysis_reg_quarter['co2_conc'] = analysis_reg_quarter['co2_conc'] - analysis_reg_quarter['co2_conc'].shift(4)
analysis_reg_quarter.dropna(inplace = True)
### Quarterly ###
analysis_reg_quarter_edited = analysis_reg_quarter.copy()
analysis_reg_quarter_edited['positive_change'] = list(map(lambda x : 0 if x <= 0 else x,analysis_reg_quarter['oil_spot_WTI'] ))
analysis_reg_quarter_edited['negative_change'] = list(map(lambda x : 0 if x >= 0 else x,analysis_reg_quarter['oil_spot_WTI'] ))
analysis_reg_quarter_edited['p+_lag1'] = analysis_reg_quarter_edited['positive_change'].shift(1)
analysis_reg_quarter_edited['p+_lag2'] = analysis_reg_quarter_edited['positive_change'].shift(2)
analysis_reg_quarter_edited['p+_lag3'] = analysis_reg_quarter_edited['positive_change'].shift(3)
analysis_reg_quarter_edited['p+_lag4'] = analysis_reg_quarter_edited['positive_change'].shift(4)
analysis_reg_quarter_edited['p+_lag5'] = analysis_reg_quarter_edited['positive_change'].shift(5)
analysis_reg_quarter_edited['p+_lag6'] = analysis_reg_quarter_edited['positive_change'].shift(6)
analysis_reg_quarter_edited['p+_lag7'] = analysis_reg_quarter_edited['positive_change'].shift(7)
analysis_reg_quarter_edited['p+_lag8'] = analysis_reg_quarter_edited['positive_change'].shift(8)
analysis_reg_quarter_edited['p+_lag9'] = analysis_reg_quarter_edited['positive_change'].shift(9)
analysis_reg_quarter_edited['p+_lag10'] = analysis_reg_quarter_edited['positive_change'].shift(10)
analysis_reg_quarter_edited['p-_lag1'] = analysis_reg_quarter_edited['negative_change'].shift(1)
analysis_reg_quarter_edited['p-_lag2'] = analysis_reg_quarter_edited['negative_change'].shift(2)
analysis_reg_quarter_edited['p-_lag3'] = analysis_reg_quarter_edited['negative_change'].shift(3)
analysis_reg_quarter_edited['p-_lag4'] = analysis_reg_quarter_edited['negative_change'].shift(4)
analysis_reg_quarter_edited['p-_lag5'] = analysis_reg_quarter_edited['negative_change'].shift(5)
analysis_reg_quarter_edited['p-_lag6'] = analysis_reg_quarter_edited['negative_change'].shift(6)
analysis_reg_quarter_edited['p-_lag7'] = analysis_reg_quarter_edited['negative_change'].shift(7)
analysis_reg_quarter_edited['p-_lag8'] = analysis_reg_quarter_edited['negative_change'].shift(8)
analysis_reg_quarter_edited['p-_lag9'] = analysis_reg_quarter_edited['negative_change'].shift(9)
analysis_reg_quarter_edited['p-_lag10'] = analysis_reg_quarter_edited['negative_change'].shift(10)
analysis_reg_quarter_edited = analysis_reg_quarter_edited.dropna()
#Quarterly gas
x = analysis_reg_quarter_edited[['positive_change', 'negative_change','p+_lag1', 'p+_lag2',
'p+_lag3', 'p+_lag4', 'p+_lag5', 'p+_lag6', 'p+_lag7', 'p+_lag8',
'p+_lag9', 'p+_lag10', 'p-_lag1', 'p-_lag2', 'p-_lag3', 'p-_lag4',
'p-_lag5', 'p-_lag6', 'p-_lag7', 'p-_lag8', 'p-_lag9', 'p-_lag10']]
reg_model = sm.OLS(analysis_reg_quarter_edited['Gasoline_Price_US'],x)
result = reg_model.fit()
print(result.summary())
## Three years of emissions
result.tvalues.to_frame()
#Quarterly oil futuress to future gasoline
#Quarterly
analysis = oil_co2_df.copy()
oil_co2_df.columns
analysis_quarter = analysis.query('Month in [12,3,6,9]').groupby('Par_Month').last()[['future_oil_contract_1','Gasoline_Price_US','co2_conc']]
analysis_reg_quarter = np.log(analysis_quarter) - np.log(analysis_quarter.shift(1))
analysis_reg_quarter.dropna(inplace = True)
analysis_reg_quarter['co2_conc'] = analysis_reg_quarter['co2_conc'] - analysis_reg_quarter['co2_conc'].shift(4)
analysis_reg_quarter.dropna(inplace = True)
### Quarterly ###
analysis_reg_quarter_edited = analysis_reg_quarter.copy()
analysis_reg_quarter_edited['positive_change'] = list(map(lambda x : 0 if x <= 0 else x,analysis_reg_quarter['future_oil_contract_1']))
analysis_reg_quarter_edited['negative_change'] = list(map(lambda x : 0 if x >= 0 else x,analysis_reg_quarter['future_oil_contract_1']))
analysis_reg_quarter_edited['p+_lag1'] = analysis_reg_quarter_edited['positive_change'].shift(1)
analysis_reg_quarter_edited['p+_lag2'] = analysis_reg_quarter_edited['positive_change'].shift(2)
analysis_reg_quarter_edited['p+_lag3'] = analysis_reg_quarter_edited['positive_change'].shift(3)
analysis_reg_quarter_edited['p+_lag4'] = analysis_reg_quarter_edited['positive_change'].shift(4)
analysis_reg_quarter_edited['p+_lag5'] = analysis_reg_quarter_edited['positive_change'].shift(5)
analysis_reg_quarter_edited['p+_lag6'] = analysis_reg_quarter_edited['positive_change'].shift(6)
analysis_reg_quarter_edited['p+_lag7'] = analysis_reg_quarter_edited['positive_change'].shift(7)
analysis_reg_quarter_edited['p+_lag8'] = analysis_reg_quarter_edited['positive_change'].shift(8)
analysis_reg_quarter_edited['p+_lag9'] = analysis_reg_quarter_edited['positive_change'].shift(9)
analysis_reg_quarter_edited['p+_lag10'] = analysis_reg_quarter_edited['positive_change'].shift(10)
analysis_reg_quarter_edited['chg_future_gasoline_lag-1'] = analysis_reg_quarter_edited['Gasoline_Price_US'].shift(-1)
analysis_reg_quarter_edited['p-_lag1'] = analysis_reg_quarter_edited['negative_change'].shift(1)
analysis_reg_quarter_edited['p-_lag2'] = analysis_reg_quarter_edited['negative_change'].shift(2)
analysis_reg_quarter_edited['p-_lag3'] = analysis_reg_quarter_edited['negative_change'].shift(3)
analysis_reg_quarter_edited['p-_lag4'] = analysis_reg_quarter_edited['negative_change'].shift(4)
analysis_reg_quarter_edited['p-_lag5'] = analysis_reg_quarter_edited['negative_change'].shift(5)
analysis_reg_quarter_edited['p-_lag6'] = analysis_reg_quarter_edited['negative_change'].shift(6)
analysis_reg_quarter_edited['p-_lag7'] = analysis_reg_quarter_edited['negative_change'].shift(7)
analysis_reg_quarter_edited['p-_lag8'] = analysis_reg_quarter_edited['negative_change'].shift(8)
analysis_reg_quarter_edited['p-_lag9'] = analysis_reg_quarter_edited['negative_change'].shift(9)
analysis_reg_quarter_edited['p-_lag10'] = analysis_reg_quarter_edited['negative_change'].shift(10)
analysis_reg_quarter_edited = analysis_reg_quarter_edited.dropna()
#Quarterly gas
x = analysis_reg_quarter_edited[['positive_change', 'negative_change','p+_lag1', 'p+_lag2',
'p+_lag3', 'p+_lag4', 'p+_lag5', 'p+_lag6', 'p+_lag7', 'p+_lag8',
'p+_lag9', 'p+_lag10', 'p-_lag1', 'p-_lag2', 'p-_lag3', 'p-_lag4',
'p-_lag5', 'p-_lag6', 'p-_lag7', 'p-_lag8', 'p-_lag9', 'p-_lag10']]
reg_model = sm.OLS(analysis_reg_quarter_edited['chg_future_gasoline_lag-1'],x)
result = reg_model.fit()
print(result.summary())
## Three years of emissions
result.tvalues.to_frame()
#Quarterly CO2
x = analysis_reg_quarter_edited[['positive_change', 'negative_change','p+_lag1', 'p+_lag2',
'p+_lag3', 'p+_lag4', 'p+_lag5', 'p+_lag6', 'p+_lag7', 'p+_lag8',
'p+_lag9', 'p+_lag10', 'p-_lag1', 'p-_lag2', 'p-_lag3', 'p-_lag4',
'p-_lag5', 'p-_lag6', 'p-_lag7', 'p-_lag8', 'p-_lag9', 'p-_lag10']]
reg_model = sm.OLS(analysis_reg_quarter_edited['co2_conc'],x)
result = reg_model.fit()
print(result.summary())
## Three years of emissions
result.tvalues.to_frame()
### Quarterly CO2 ###
### Quarterly Lag 0###
analysis = oil_co2_df.copy()
oil_co2_df.columns
analysis_quarter = analysis.query('Month in [12,3,6,9]').groupby('Par_Month').last()[['oil_spot_WTI','Gasoline_Price_US','co2_conc']]
analysis_reg_quarter = np.log(analysis_quarter) - np.log(analysis_quarter.shift(1))
analysis_reg_quarter.dropna(inplace = True)
analysis_reg_quarter['co2_conc'] = analysis_reg_quarter['co2_conc'] - analysis_reg_quarter['co2_conc'].shift(4)
analysis_reg_quarter.dropna(inplace = True)
### Quarterly ###
analysis_reg_quarter_edited = analysis_reg_quarter.copy()
analysis_reg_quarter_edited['chg_oil_spot_WTI_Positive'] = list(map(lambda x : 0 if x <= 0 else x,analysis_reg_quarter['oil_spot_WTI'] ))
analysis_reg_quarter_edited['chg_oil_spot_WTI_Negative'] = list(map(lambda x : 0 if x >= 0 else x,analysis_reg_quarter['oil_spot_WTI'] ))
analysis_reg = analysis_reg_quarter_edited.copy()
x = analysis_reg[['chg_oil_spot_WTI_Positive', 'chg_oil_spot_WTI_Negative']]
reg_model = sm.OLS(analysis_reg['Gasoline_Price_US'],x)
result = reg_model.fit()
print(result.summary())
### Quarterly ###
### Quarterly Lag 3###
analysis = oil_co2_df.copy()
oil_co2_df.columns
analysis_quarter = analysis.query('Month in [12,3,6,9]').groupby('Par_Month').last()[['oil_spot_WTI','Gasoline_Price_US','co2_conc']]
analysis_reg_quarter = np.log(analysis_quarter) - np.log(analysis_quarter.shift(1))
analysis_reg_quarter.dropna(inplace = True)
analysis_reg_quarter['co2_conc'] = analysis_reg_quarter['co2_conc'] - analysis_reg_quarter['co2_conc'].shift(4)
analysis_reg_quarter.dropna(inplace = True)
### Quarterly ###
analysis_reg_quarter_edited = analysis_reg_quarter.copy()
analysis_reg_quarter_edited['chg_oil_spot_WTI_Positive'] = list(map(lambda x : 0 if x <= 0 else x,analysis_reg_quarter['oil_spot_WTI'] ))
analysis_reg_quarter_edited['chg_oil_spot_WTI_Negative'] = list(map(lambda x : 0 if x >= 0 else x,analysis_reg_quarter['oil_spot_WTI'] ))
analysis_reg = analysis_reg_quarter_edited.copy()
analysis_reg['p+_lag1'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Positive'].shift(1)
analysis_reg['p+_lag2'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Positive'].shift(2)
analysis_reg['p+_lag3'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Positive'].shift(3)
analysis_reg['p-_lag1'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Negative'].shift(1)
analysis_reg['p-_lag2'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Negative'].shift(2)
analysis_reg['p-_lag3'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Negative'].shift(3)
analysis_reg = analysis_reg.dropna(subset = ['p+_lag1','p-_lag1','p+_lag2','p-_lag2','p+_lag3','p-_lag3'])
x = analysis_reg[['chg_oil_spot_WTI_Positive', 'chg_oil_spot_WTI_Negative','p+_lag1','p-_lag1','p+_lag2','p-_lag2','p+_lag3','p-_lag3']]
reg_model = sm.OLS(analysis_reg['Gasoline_Price_US'],x)
result = reg_model.fit()
print(result.summary())
### Quarterly ###
### Quarterly Lag 6###
analysis = oil_co2_df.copy()
oil_co2_df.columns
analysis = oil_co2_df.copy()
oil_co2_df.columns
analysis_quarter = analysis.query('Month in [12,3,6,9]').groupby('Par_Month').last()[['oil_spot_WTI','Gasoline_Price_US','co2_conc']]
analysis_reg_quarter = np.log(analysis_quarter) - np.log(analysis_quarter.shift(1))
analysis_reg_quarter.dropna(inplace = True)
analysis_reg_quarter['co2_conc'] = analysis_reg_quarter['co2_conc'] - analysis_reg_quarter['co2_conc'].shift(4)
analysis_reg_quarter.dropna(inplace = True)
### Quarterly ###
analysis_reg_quarter_edited = analysis_reg_quarter.copy()
analysis_reg_quarter_edited['chg_oil_spot_WTI_Positive'] = list(map(lambda x : 0 if x <= 0 else x,analysis_reg_quarter['oil_spot_WTI'] ))
analysis_reg_quarter_edited['chg_oil_spot_WTI_Negative'] = list(map(lambda x : 0 if x >= 0 else x,analysis_reg_quarter['oil_spot_WTI'] ))
analysis_reg = analysis_reg_quarter_edited.copy()
analysis_reg['p+_lag1'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Positive'].shift(1)
analysis_reg['p+_lag2'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Positive'].shift(2)
analysis_reg['p+_lag3'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Positive'].shift(3)
analysis_reg['p+_lag4'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Positive'].shift(4)
analysis_reg['p+_lag5'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Positive'].shift(5)
analysis_reg['p+_lag6'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Positive'].shift(6)
analysis_reg['p-_lag1'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Negative'].shift(1)
analysis_reg['p-_lag2'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Negative'].shift(2)
analysis_reg['p-_lag3'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Negative'].shift(3)
analysis_reg['p-_lag4'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Negative'].shift(4)
analysis_reg['p-_lag5'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Negative'].shift(5)
analysis_reg['p-_lag6'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Negative'].shift(6)
analysis_reg = analysis_reg.dropna(subset = ['p+_lag1','p-_lag1','p+_lag2','p-_lag2','p+_lag3','p-_lag3','p+_lag4','p-_lag4','p+_lag5','p-_lag5','p+_lag6','p-_lag6'])
x = analysis_reg[['chg_oil_spot_WTI_Positive', 'chg_oil_spot_WTI_Negative','p+_lag1','p-_lag1','p+_lag2','p-_lag2','p+_lag3','p-_lag3','p+_lag4','p-_lag4','p+_lag5','p-_lag5','p+_lag6','p-_lag6']]
reg_model = sm.OLS(analysis_reg['Gasoline_Price_US'],x)
result = reg_model.fit()
print(result.summary())
### quarterly ###
### Quarterly Lag 12###
analysis = oil_co2_df.copy()
oil_co2_df.columns
analysis_quarter = analysis.query('Month in [12,3,6,9]').groupby('Par_Month').last()[['oil_spot_WTI','Gasoline_Price_US','co2_conc']]
analysis_reg_quarter = np.log(analysis_quarter) - np.log(analysis_quarter.shift(1))
analysis_reg_quarter.dropna(inplace = True)
analysis_reg_quarter['co2_conc'] = analysis_reg_quarter['co2_conc'] - analysis_reg_quarter['co2_conc'].shift(4)
analysis_reg_quarter.dropna(inplace = True)
### Quarterly ###
analysis_reg_quarter_edited = analysis_reg_quarter.copy()
analysis_reg_quarter_edited['chg_oil_spot_WTI_Positive'] = list(map(lambda x : 0 if x <= 0 else x,analysis_reg_quarter['oil_spot_WTI'] ))
analysis_reg_quarter_edited['chg_oil_spot_WTI_Negative'] = list(map(lambda x : 0 if x >= 0 else x,analysis_reg_quarter['oil_spot_WTI'] ))
analysis_reg = analysis_reg_quarter_edited.copy()
analysis_reg['p+_lag1'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Positive'].shift(1)
analysis_reg['p+_lag2'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Positive'].shift(2)
analysis_reg['p+_lag3'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Positive'].shift(3)
analysis_reg['p+_lag4'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Positive'].shift(4)
analysis_reg['p+_lag5'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Positive'].shift(5)
analysis_reg['p+_lag6'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Positive'].shift(6)
analysis_reg['p+_lag7'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Positive'].shift(7)
analysis_reg['p+_lag8'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Positive'].shift(8)
analysis_reg['p+_lag9'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Positive'].shift(9)
analysis_reg['p+_lag10'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Positive'].shift(10)
analysis_reg['p+_lag11'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Positive'].shift(11)
analysis_reg['p+_lag12'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Positive'].shift(12)
analysis_reg['p-_lag1'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Negative'].shift(1)
analysis_reg['p-_lag2'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Negative'].shift(2)
analysis_reg['p-_lag3'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Negative'].shift(3)
analysis_reg['p-_lag4'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Negative'].shift(4)
analysis_reg['p-_lag5'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Negative'].shift(5)
analysis_reg['p-_lag6'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Negative'].shift(6)
analysis_reg['p-_lag7'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Negative'].shift(7)
analysis_reg['p-_lag8'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Negative'].shift(8)
analysis_reg['p-_lag9'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Negative'].shift(9)
analysis_reg['p-_lag10'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Negative'].shift(10)
analysis_reg['p-_lag11'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Negative'].shift(11)
analysis_reg['p-_lag12'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Negative'].shift(12)
analysis_reg = analysis_reg.dropna(subset = ['p+_lag1','p-_lag1','p+_lag2','p-_lag2','p+_lag3','p-_lag3','p+_lag4','p-_lag4','p+_lag5','p-_lag5','p+_lag6','p-_lag6','p+_lag7','p-_lag7','p+_lag8','p-_lag8','p+_lag9','p-_lag9','p+_lag10','p-_lag10','p+_lag11','p-_lag11','p+_lag12','p-_lag12'])
x = analysis_reg[['chg_oil_spot_WTI_Positive', 'chg_oil_spot_WTI_Negative','p+_lag1','p-_lag1','p+_lag2','p-_lag2','p+_lag3','p-_lag3','p+_lag4','p-_lag4','p+_lag5','p-_lag5','p+_lag6','p-_lag6','p+_lag7','p-_lag7','p+_lag8','p-_lag8','p+_lag9','p-_lag9','p+_lag10','p-_lag10','p+_lag11','p-_lag11','p+_lag12','p-_lag12']]
reg_model = sm.OLS(analysis_reg['Gasoline_Price_US'],x)
result = reg_model.fit()
print(result.summary())
### Quarterly ###
### Quarterly Lag 24###
analysis = oil_co2_df.copy()
oil_co2_df.columns
analysis_quarter = analysis.query('Month in [12,3,6,9]').groupby('Par_Month').last()[['oil_spot_WTI','Gasoline_Price_US','co2_conc']]
analysis_reg_quarter = np.log(analysis_quarter) - np.log(analysis_quarter.shift(1))
analysis_reg_quarter.dropna(inplace = True)
analysis_reg_quarter['co2_conc'] = analysis_reg_quarter['co2_conc'] - analysis_reg_quarter['co2_conc'].shift(4)
analysis_reg_quarter.dropna(inplace = True)
### Quarterly ###
analysis_reg_quarter_edited = analysis_reg_quarter.copy()
analysis_reg_quarter_edited['chg_oil_spot_WTI_Positive'] = list(map(lambda x : 0 if x <= 0 else x,analysis_reg_quarter['oil_spot_WTI'] ))
analysis_reg_quarter_edited['chg_oil_spot_WTI_Negative'] = list(map(lambda x : 0 if x >= 0 else x,analysis_reg_quarter['oil_spot_WTI'] ))
analysis_reg = analysis_reg_quarter_edited.copy()
analysis_reg['p+_lag1'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Positive'].shift(1)
analysis_reg['p+_lag2'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Positive'].shift(2)
analysis_reg['p+_lag3'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Positive'].shift(3)
analysis_reg['p+_lag4'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Positive'].shift(4)
analysis_reg['p+_lag5'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Positive'].shift(5)
analysis_reg['p+_lag6'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Positive'].shift(6)
analysis_reg['p+_lag7'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Positive'].shift(7)
analysis_reg['p+_lag8'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Positive'].shift(8)
analysis_reg['p+_lag9'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Positive'].shift(9)
analysis_reg['p+_lag10'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Positive'].shift(10)
analysis_reg['p+_lag11'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Positive'].shift(11)
analysis_reg['p+_lag12'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Positive'].shift(12)
analysis_reg['p+_lag13'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Positive'].shift(13)
analysis_reg['p+_lag14'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Positive'].shift(14)
analysis_reg['p+_lag15'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Positive'].shift(15)
analysis_reg['p+_lag16'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Positive'].shift(16)
analysis_reg['p+_lag17'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Positive'].shift(17)
analysis_reg['p+_lag18'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Positive'].shift(18)
analysis_reg['p+_lag19'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Positive'].shift(19)
analysis_reg['p+_lag20'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Positive'].shift(20)
analysis_reg['p+_lag21'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Positive'].shift(21)
analysis_reg['p+_lag22'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Positive'].shift(22)
analysis_reg['p+_lag23'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Positive'].shift(23)
analysis_reg['p+_lag24'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Positive'].shift(24)
analysis_reg['p-_lag1'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Negative'].shift(1)
analysis_reg['p-_lag2'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Negative'].shift(2)
analysis_reg['p-_lag3'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Negative'].shift(3)
analysis_reg['p-_lag4'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Negative'].shift(4)
analysis_reg['p-_lag5'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Negative'].shift(5)
analysis_reg['p-_lag6'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Negative'].shift(6)
analysis_reg['p-_lag7'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Negative'].shift(7)
analysis_reg['p-_lag8'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Negative'].shift(8)
analysis_reg['p-_lag9'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Negative'].shift(9)
analysis_reg['p-_lag10'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Negative'].shift(10)
analysis_reg['p-_lag11'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Negative'].shift(11)
analysis_reg['p-_lag12'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Negative'].shift(12)
analysis_reg['p-_lag13'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Negative'].shift(13)
analysis_reg['p-_lag14'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Negative'].shift(14)
analysis_reg['p-_lag15'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Negative'].shift(15)
analysis_reg['p-_lag16'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Negative'].shift(16)
analysis_reg['p-_lag17'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Negative'].shift(17)
analysis_reg['p-_lag18'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Negative'].shift(18)
analysis_reg['p-_lag19'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Negative'].shift(19)
analysis_reg['p-_lag20'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Negative'].shift(20)
analysis_reg['p-_lag21'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Negative'].shift(21)
analysis_reg['p-_lag22'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Negative'].shift(22)
analysis_reg['p-_lag23'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Negative'].shift(23)
analysis_reg['p-_lag24'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Negative'].shift(24)
analysis_reg = analysis_reg.dropna(subset = ['p+_lag1','p-_lag1','p+_lag2','p-_lag2','p+_lag3','p-_lag3','p+_lag4','p-_lag4','p+_lag5','p-_lag5','p+_lag6','p-_lag6','p+_lag7','p-_lag7','p+_lag8','p-_lag8','p+_lag9','p-_lag9','p+_lag10','p-_lag10','p+_lag11','p-_lag11','p+_lag12','p-_lag12'
,'p+_lag13','p-_lag13','p+_lag14','p-_lag14','p+_lag15','p-_lag15','p+_lag16','p-_lag16','p+_lag17','p-_lag17','p+_lag18','p-_lag18','p+_lag19','p-_lag19','p+_lag20','p-_lag20','p+_lag21','p-_lag21','p+_lag22','p-_lag22','p+_lag23','p-_lag23','p+_lag24','p-_lag24'])
x = analysis_reg[['chg_oil_spot_WTI_Positive', 'chg_oil_spot_WTI_Negative','p+_lag1','p-_lag1','p+_lag2','p-_lag2','p+_lag3','p-_lag3','p+_lag4','p-_lag4','p+_lag5','p-_lag5','p+_lag6','p-_lag6','p+_lag7','p-_lag7','p+_lag8','p-_lag8','p+_lag9','p-_lag9','p+_lag10','p-_lag10','p+_lag11','p-_lag11','p+_lag12','p-_lag12'
,'p+_lag13','p-_lag13','p+_lag14','p-_lag14','p+_lag15','p-_lag15','p+_lag16','p-_lag16','p+_lag17','p-_lag17','p+_lag18','p-_lag18','p+_lag19','p-_lag19','p+_lag20','p-_lag20','p+_lag21','p-_lag21','p+_lag22','p-_lag22','p+_lag23','p-_lag23','p+_lag24','p-_lag24']]
reg_model = sm.OLS(analysis_reg['Gasoline_Price_US'],x)
result = reg_model.fit()
print(result.summary())
### Quarterly CO2 ###
### Quarterly Lag 0###
analysis = oil_co2_df.copy()
oil_co2_df.columns
analysis_quarter = analysis.query('Month in [12,3,6,9]').groupby('Par_Month').last()[['oil_spot_WTI','Gasoline_Price_US','co2_conc']]
analysis_reg_quarter = np.log(analysis_quarter) - np.log(analysis_quarter.shift(1))
analysis_reg_quarter.dropna(inplace = True)
analysis_reg_quarter['co2_conc'] = analysis_reg_quarter['co2_conc'] - analysis_reg_quarter['co2_conc'].shift(4)
analysis_reg_quarter.dropna(inplace = True)
### Quarterly ###
analysis_reg_quarter_edited = analysis_reg_quarter.copy()
analysis_reg_quarter_edited['chg_oil_spot_WTI_Positive'] = list(map(lambda x : 0 if x <= 0 else x,analysis_reg_quarter['oil_spot_WTI'] ))
analysis_reg_quarter_edited['chg_oil_spot_WTI_Negative'] = list(map(lambda x : 0 if x >= 0 else x,analysis_reg_quarter['oil_spot_WTI'] ))
analysis_reg = analysis_reg_quarter_edited.copy()
x = analysis_reg[['chg_oil_spot_WTI_Positive', 'chg_oil_spot_WTI_Negative']]
reg_model = sm.OLS(analysis_reg['co2_conc'],x)
result = reg_model.fit()
print(result.summary())
### Quarterly ###
### Quarterly Lag 3###
analysis = oil_co2_df.copy()
oil_co2_df.columns
analysis_quarter = analysis.query('Month in [12,3,6,9]').groupby('Par_Month').last()[['oil_spot_WTI','Gasoline_Price_US','co2_conc']]
analysis_reg_quarter = np.log(analysis_quarter) - np.log(analysis_quarter.shift(1))
analysis_reg_quarter.dropna(inplace = True)
analysis_reg_quarter['co2_conc'] = analysis_reg_quarter['co2_conc'] - analysis_reg_quarter['co2_conc'].shift(4)
analysis_reg_quarter.dropna(inplace = True)
### Quarterly ###
analysis_reg_quarter_edited = analysis_reg_quarter.copy()
analysis_reg_quarter_edited['chg_oil_spot_WTI_Positive'] = list(map(lambda x : 0 if x <= 0 else x,analysis_reg_quarter['oil_spot_WTI'] ))
analysis_reg_quarter_edited['chg_oil_spot_WTI_Negative'] = list(map(lambda x : 0 if x >= 0 else x,analysis_reg_quarter['oil_spot_WTI'] ))
analysis_reg = analysis_reg_quarter_edited.copy()
analysis_reg['p+_lag1'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Positive'].shift(1)
analysis_reg['p+_lag2'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Positive'].shift(2)
analysis_reg['p+_lag3'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Positive'].shift(3)
analysis_reg['p-_lag1'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Negative'].shift(1)
analysis_reg['p-_lag2'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Negative'].shift(2)
analysis_reg['p-_lag3'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Negative'].shift(3)
analysis_reg = analysis_reg.dropna(subset = ['p+_lag1','p-_lag1','p+_lag2','p-_lag2','p+_lag3','p-_lag3'])
x = analysis_reg[['chg_oil_spot_WTI_Positive', 'chg_oil_spot_WTI_Negative','p+_lag1','p-_lag1','p+_lag2','p-_lag2','p+_lag3','p-_lag3']]
reg_model = sm.OLS(analysis_reg['co2_conc'],x)
result = reg_model.fit()
print(result.summary())
### Quarterly ###
### Quarterly Lag 6###
analysis = oil_co2_df.copy()
oil_co2_df.columns
analysis = oil_co2_df.copy()
oil_co2_df.columns
analysis_quarter = analysis.query('Month in [12,3,6,9]').groupby('Par_Month').last()[['oil_spot_WTI','Gasoline_Price_US','co2_conc']]
analysis_reg_quarter = np.log(analysis_quarter) - np.log(analysis_quarter.shift(1))
analysis_reg_quarter.dropna(inplace = True)
analysis_reg_quarter['co2_conc'] = analysis_reg_quarter['co2_conc'] - analysis_reg_quarter['co2_conc'].shift(4)
analysis_reg_quarter.dropna(inplace = True)
### Quarterly ###
analysis_reg_quarter_edited = analysis_reg_quarter.copy()
analysis_reg_quarter_edited['chg_oil_spot_WTI_Positive'] = list(map(lambda x : 0 if x <= 0 else x,analysis_reg_quarter['oil_spot_WTI'] ))
analysis_reg_quarter_edited['chg_oil_spot_WTI_Negative'] = list(map(lambda x : 0 if x >= 0 else x,analysis_reg_quarter['oil_spot_WTI'] ))
analysis_reg = analysis_reg_quarter_edited.copy()
analysis_reg['p+_lag1'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Positive'].shift(1)
analysis_reg['p+_lag2'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Positive'].shift(2)
analysis_reg['p+_lag3'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Positive'].shift(3)
analysis_reg['p+_lag4'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Positive'].shift(4)
analysis_reg['p+_lag5'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Positive'].shift(5)
analysis_reg['p+_lag6'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Positive'].shift(6)
analysis_reg['p-_lag1'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Negative'].shift(1)
analysis_reg['p-_lag2'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Negative'].shift(2)
analysis_reg['p-_lag3'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Negative'].shift(3)
analysis_reg['p-_lag4'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Negative'].shift(4)
analysis_reg['p-_lag5'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Negative'].shift(5)
analysis_reg['p-_lag6'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Negative'].shift(6)
analysis_reg = analysis_reg.dropna(subset = ['p+_lag1','p-_lag1','p+_lag2','p-_lag2','p+_lag3','p-_lag3','p+_lag4','p-_lag4','p+_lag5','p-_lag5','p+_lag6','p-_lag6'])
x = analysis_reg[['chg_oil_spot_WTI_Positive', 'chg_oil_spot_WTI_Negative','p+_lag1','p-_lag1','p+_lag2','p-_lag2','p+_lag3','p-_lag3','p+_lag4','p-_lag4','p+_lag5','p-_lag5','p+_lag6','p-_lag6']]
reg_model = sm.OLS(analysis_reg['co2_conc'],x)
result = reg_model.fit()
print(result.summary())
### quarterly ###
### Quarterly Lag 12###
analysis = oil_co2_df.copy()
oil_co2_df.columns
analysis_quarter = analysis.query('Month in [12,3,6,9]').groupby('Par_Month').last()[['oil_spot_WTI','Gasoline_Price_US','co2_conc']]
analysis_reg_quarter = np.log(analysis_quarter) - np.log(analysis_quarter.shift(1))
analysis_reg_quarter.dropna(inplace = True)
analysis_reg_quarter['co2_conc'] = analysis_reg_quarter['co2_conc'] - analysis_reg_quarter['co2_conc'].shift(4)
analysis_reg_quarter.dropna(inplace = True)
### Quarterly ###
analysis_reg_quarter_edited = analysis_reg_quarter.copy()
analysis_reg_quarter_edited['chg_oil_spot_WTI_Positive'] = list(map(lambda x : 0 if x <= 0 else x,analysis_reg_quarter['oil_spot_WTI'] ))
analysis_reg_quarter_edited['chg_oil_spot_WTI_Negative'] = list(map(lambda x : 0 if x >= 0 else x,analysis_reg_quarter['oil_spot_WTI'] ))
analysis_reg = analysis_reg_quarter_edited.copy()
analysis_reg['p+_lag1'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Positive'].shift(1)
analysis_reg['p+_lag2'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Positive'].shift(2)
analysis_reg['p+_lag3'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Positive'].shift(3)
analysis_reg['p+_lag4'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Positive'].shift(4)
analysis_reg['p+_lag5'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Positive'].shift(5)
analysis_reg['p+_lag6'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Positive'].shift(6)
analysis_reg['p+_lag7'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Positive'].shift(7)
analysis_reg['p+_lag8'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Positive'].shift(8)
analysis_reg['p+_lag9'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Positive'].shift(9)
analysis_reg['p+_lag10'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Positive'].shift(10)
analysis_reg['p+_lag11'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Positive'].shift(11)
analysis_reg['p+_lag12'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Positive'].shift(12)
analysis_reg['p-_lag1'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Negative'].shift(1)
analysis_reg['p-_lag2'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Negative'].shift(2)
analysis_reg['p-_lag3'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Negative'].shift(3)
analysis_reg['p-_lag4'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Negative'].shift(4)
analysis_reg['p-_lag5'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Negative'].shift(5)
analysis_reg['p-_lag6'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Negative'].shift(6)
analysis_reg['p-_lag7'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Negative'].shift(7)
analysis_reg['p-_lag8'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Negative'].shift(8)
analysis_reg['p-_lag9'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Negative'].shift(9)
analysis_reg['p-_lag10'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Negative'].shift(10)
analysis_reg['p-_lag11'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Negative'].shift(11)
analysis_reg['p-_lag12'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Negative'].shift(12)
analysis_reg = analysis_reg.dropna(subset = ['p+_lag1','p-_lag1','p+_lag2','p-_lag2','p+_lag3','p-_lag3','p+_lag4','p-_lag4','p+_lag5','p-_lag5','p+_lag6','p-_lag6','p+_lag7','p-_lag7','p+_lag8','p-_lag8','p+_lag9','p-_lag9','p+_lag10','p-_lag10','p+_lag11','p-_lag11','p+_lag12','p-_lag12'])
x = analysis_reg[['chg_oil_spot_WTI_Positive', 'chg_oil_spot_WTI_Negative','p+_lag1','p-_lag1','p+_lag2','p-_lag2','p+_lag3','p-_lag3','p+_lag4','p-_lag4','p+_lag5','p-_lag5','p+_lag6','p-_lag6','p+_lag7','p-_lag7','p+_lag8','p-_lag8','p+_lag9','p-_lag9','p+_lag10','p-_lag10','p+_lag11','p-_lag11','p+_lag12','p-_lag12']]
reg_model = sm.OLS(analysis_reg['co2_conc'],x)
result = reg_model.fit()
print(result.summary())
### Quarterly ###
### Quarterly Lag 24###
analysis = oil_co2_df.copy()
oil_co2_df.columns
analysis_quarter = analysis.query('Month in [12,3,6,9]').groupby('Par_Month').last()[['oil_spot_WTI','Gasoline_Price_US','co2_conc']]
analysis_reg_quarter = np.log(analysis_quarter) - np.log(analysis_quarter.shift(1))
analysis_reg_quarter.dropna(inplace = True)
analysis_reg_quarter['co2_conc'] = analysis_reg_quarter['co2_conc'] - analysis_reg_quarter['co2_conc'].shift(4)
analysis_reg_quarter.dropna(inplace = True)
### Quarterly ###
analysis_reg_quarter_edited = analysis_reg_quarter.copy()
analysis_reg_quarter_edited['chg_oil_spot_WTI_Positive'] = list(map(lambda x : 0 if x <= 0 else x,analysis_reg_quarter['oil_spot_WTI'] ))
analysis_reg_quarter_edited['chg_oil_spot_WTI_Negative'] = list(map(lambda x : 0 if x >= 0 else x,analysis_reg_quarter['oil_spot_WTI'] ))
analysis_reg = analysis_reg_quarter_edited.copy()
analysis_reg['p+_lag1'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Positive'].shift(1)
analysis_reg['p+_lag2'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Positive'].shift(2)
analysis_reg['p+_lag3'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Positive'].shift(3)
analysis_reg['p+_lag4'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Positive'].shift(4)
analysis_reg['p+_lag5'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Positive'].shift(5)
analysis_reg['p+_lag6'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Positive'].shift(6)
analysis_reg['p+_lag7'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Positive'].shift(7)
analysis_reg['p+_lag8'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Positive'].shift(8)
analysis_reg['p+_lag9'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Positive'].shift(9)
analysis_reg['p+_lag10'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Positive'].shift(10)
analysis_reg['p+_lag11'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Positive'].shift(11)
analysis_reg['p+_lag12'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Positive'].shift(12)
analysis_reg['p+_lag13'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Positive'].shift(13)
analysis_reg['p+_lag14'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Positive'].shift(14)
analysis_reg['p+_lag15'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Positive'].shift(15)
analysis_reg['p+_lag16'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Positive'].shift(16)
analysis_reg['p+_lag17'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Positive'].shift(17)
analysis_reg['p+_lag18'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Positive'].shift(18)
analysis_reg['p+_lag19'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Positive'].shift(19)
analysis_reg['p+_lag20'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Positive'].shift(20)
analysis_reg['p+_lag21'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Positive'].shift(21)
analysis_reg['p+_lag22'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Positive'].shift(22)
analysis_reg['p+_lag23'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Positive'].shift(23)
analysis_reg['p+_lag24'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Positive'].shift(24)
analysis_reg['p-_lag1'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Negative'].shift(1)
analysis_reg['p-_lag2'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Negative'].shift(2)
analysis_reg['p-_lag3'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Negative'].shift(3)
analysis_reg['p-_lag4'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Negative'].shift(4)
analysis_reg['p-_lag5'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Negative'].shift(5)
analysis_reg['p-_lag6'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Negative'].shift(6)
analysis_reg['p-_lag7'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Negative'].shift(7)
analysis_reg['p-_lag8'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Negative'].shift(8)
analysis_reg['p-_lag9'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Negative'].shift(9)
analysis_reg['p-_lag10'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Negative'].shift(10)
analysis_reg['p-_lag11'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Negative'].shift(11)
analysis_reg['p-_lag12'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Negative'].shift(12)
analysis_reg['p-_lag13'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Negative'].shift(13)
analysis_reg['p-_lag14'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Negative'].shift(14)
analysis_reg['p-_lag15'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Negative'].shift(15)
analysis_reg['p-_lag16'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Negative'].shift(16)
analysis_reg['p-_lag17'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Negative'].shift(17)
analysis_reg['p-_lag18'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Negative'].shift(18)
analysis_reg['p-_lag19'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Negative'].shift(19)
analysis_reg['p-_lag20'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Negative'].shift(20)
analysis_reg['p-_lag21'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Negative'].shift(21)
analysis_reg['p-_lag22'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Negative'].shift(22)
analysis_reg['p-_lag23'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Negative'].shift(23)
analysis_reg['p-_lag24'] = analysis_reg_quarter_edited['chg_oil_spot_WTI_Negative'].shift(24)
analysis_reg = analysis_reg.dropna(subset = ['p+_lag1','p-_lag1','p+_lag2','p-_lag2','p+_lag3','p-_lag3','p+_lag4','p-_lag4','p+_lag5','p-_lag5','p+_lag6','p-_lag6','p+_lag7','p-_lag7','p+_lag8','p-_lag8','p+_lag9','p-_lag9','p+_lag10','p-_lag10','p+_lag11','p-_lag11','p+_lag12','p-_lag12'
,'p+_lag13','p-_lag13','p+_lag14','p-_lag14','p+_lag15','p-_lag15','p+_lag16','p-_lag16','p+_lag17','p-_lag17','p+_lag18','p-_lag18','p+_lag19','p-_lag19','p+_lag20','p-_lag20','p+_lag21','p-_lag21','p+_lag22','p-_lag22','p+_lag23','p-_lag23','p+_lag24','p-_lag24'])
x = analysis_reg[['chg_oil_spot_WTI_Positive', 'chg_oil_spot_WTI_Negative','p+_lag1','p-_lag1','p+_lag2','p-_lag2','p+_lag3','p-_lag3','p+_lag4','p-_lag4','p+_lag5','p-_lag5','p+_lag6','p-_lag6','p+_lag7','p-_lag7','p+_lag8','p-_lag8','p+_lag9','p-_lag9','p+_lag10','p-_lag10','p+_lag11','p-_lag11','p+_lag12','p-_lag12'
,'p+_lag13','p-_lag13','p+_lag14','p-_lag14','p+_lag15','p-_lag15','p+_lag16','p-_lag16','p+_lag17','p-_lag17','p+_lag18','p-_lag18','p+_lag19','p-_lag19','p+_lag20','p-_lag20','p+_lag21','p-_lag21','p+_lag22','p-_lag22','p+_lag23','p-_lag23','p+_lag24','p-_lag24']]
reg_model = sm.OLS(analysis_reg['co2_conc'],x)
result = reg_model.fit()
print(result.summary())
main_frame_granger = oil_co2_df.copy()
oil_price_month = main_frame_granger['Month'].to_list()
quarter = []
for i in range(len(oil_price_month)):
if oil_price_month[i] in [1,2,3]:
#print(oil_price_month[i])
quarter.append(1)
elif oil_price_month[i] in [4,5,6]:
quarter.append(2)
elif oil_price_month[i] in [7,8,9]:
quarter.append(3)
elif oil_price_month[i] in [10,11,12]:
quarter.append(4)
else:
quarter.append(oil_price_month[i])
main_frame_granger['Quarter'] = quarter
main_frame_granger = main_frame_granger.drop(columns = 'month')
main_frame_granger
# MERGE GDP FRAME
main_frame_granger = pd.merge(main_frame_granger,gdp_df, how = 'left', on = ['Year','Quarter'])
main_frame_granger = main_frame_granger.dropna(subset = ['gdp_date'])
#MERGE GLOBAL TEMPERATURE FRAME
main_frame_granger = pd.merge(main_frame_granger,glob_temp, how = 'left', on = ['Par_Month'])
main_frame_granger = main_frame_granger.drop(columns = ['Year_y','Month_x','Month_y']).rename(columns = {'Year_x' : 'Year'})
main_frame_granger_week = main_frame_granger.groupby('Weekindex').last()[[ 'Gasoline_Price_US','co2_conc','oil_spot_WTI', 'oil_spot_Brent', 'future_oil_contract_1','sugar_price_us', 'ethanol_price_us','gdp_us', 'gdp_china', 'gdp_europe', 'Celsius']]
main_frame_granger_month = main_frame_granger.groupby('Par_Month').last()[[ 'Gasoline_Price_US','co2_conc','oil_spot_WTI', 'oil_spot_Brent', 'future_oil_contract_1','sugar_price_us', 'ethanol_price_us','gdp_us', 'gdp_china', 'gdp_europe', 'Celsius']]
#Quarterly
Par_Quarter = []
for i in range(len(main_frame_granger)):
Par_Quarter.append(str(main_frame_granger.loc[i].Year) + str(main_frame_granger.loc[i].Quarter))
main_frame_granger['Par_Quarter'] = Par_Quarter
main_frame_granger_quarter = main_frame_granger.groupby('Par_Quarter').last()[[ 'Gasoline_Price_US','co2_conc','oil_spot_WTI', 'oil_spot_Brent', 'future_oil_contract_1','sugar_price_us', 'ethanol_price_us','gdp_us', 'gdp_china', 'gdp_europe', 'Celsius']]
#Weekly
main_frame_granger_week_diff = np.log(main_frame_granger_week) - np.log(main_frame_granger_week.shift(1))
main_frame_granger_week_diff.dropna(inplace = True)
#Monthly
main_frame_granger_month_diff = np.log(main_frame_granger_month) - np.log(main_frame_granger_month.shift(1))
main_frame_granger_month_diff['co2_conc'] = main_frame_granger_month_diff['co2_conc'] - main_frame_granger_month_diff['co2_conc'].shift(12)
main_frame_granger_month_diff.dropna(inplace = True)
#Quarterly
#Monthly
main_frame_granger_quarter_diff = np.log(main_frame_granger_quarter) - np.log(main_frame_granger_quarter.shift(1))
main_frame_granger_quarter_diff['co2_conc'] = main_frame_granger_quarter_diff['co2_conc'] - main_frame_granger_quarter_diff['co2_conc'].shift(4)
main_frame_granger_quarter_diff.dropna(inplace = True)
granger_test_weekly = grangers_causality_matrix(main_frame_granger_week_diff , variables = main_frame_granger_week_diff.columns)
granger_test_weekly
#Weekly
import seaborn as sns
corr =round(main_frame_granger_week_diff.corr(),2)
mask = np.zeros_like(corr, dtype=np.bool)
mask[np.triu_indices_from(mask)] = True
sns.heatmap(corr, xticklabels = corr.columns.values,yticklabels = corr.columns.values, annot = True,annot_kws = {'size':12}, cmap = 'vlag_r', mask = mask)
heat_map = plt.gcf()
heat_map.set_size_inches(15,8)
plt.xticks(fontsize = 15)
plt.yticks(fontsize = 15)
plt.show()
#Weekly Monthly timeframe
granger_test_monthly = grangers_causality_matrix(main_frame_granger_month_diff , variables = main_frame_granger_month_diff.columns)
granger_test_monthly
#Monthly
import seaborn as sns
corr =round(main_frame_granger_month_diff.corr(),2)
mask = np.zeros_like(corr, dtype=np.bool)
mask[np.triu_indices_from(mask)] = True
sns.heatmap(corr, xticklabels = corr.columns.values,yticklabels = corr.columns.values, annot = True,annot_kws = {'size':12}, cmap = 'vlag_r', mask = mask)
heat_map = plt.gcf()
heat_map.set_size_inches(15,8)
plt.xticks(fontsize = 15)
plt.yticks(fontsize = 15)
plt.show()
#Monthly timeframe
granger_test_quarterly = grangers_causality_matrix(main_frame_granger_quarter_diff , variables = main_frame_granger_week_diff.columns)
granger_test_quarterly
#Quarterly
import seaborn as sns
corr =round(granger_test_quarterly.corr(),2)
mask = np.zeros_like(corr, dtype=np.bool)
mask[np.triu_indices_from(mask)] = True
sns.heatmap(corr, xticklabels = corr.columns.values,yticklabels = corr.columns.values, annot = True,annot_kws = {'size':12}, cmap = 'vlag_r', mask = mask)
heat_map = plt.gcf()
heat_map.set_size_inches(15,8)
plt.xticks(fontsize = 15)
plt.yticks(fontsize = 15)
plt.show()
#Weekly Monthly timeframe
main_frame_granger_week_diff[['index']] = 0
main_frame_granger_month_diff[['index']] = 1
main_frame_granger_quarter_diff[['index']] = 2
merge_df = [main_frame_granger_week_diff,main_frame_granger_month_diff,main_frame_granger_quarter_diff]
merge_result = pd.concat(merge_df)
merge_result_w_index = merge_result.set_index('index')
merge_result = merge_result.rename(columns = {'Gasoline_Price_US': 'Gasoline Price US','co2_conc': 'CO2 Concentration','oil_spot_WTI':'Oil Spot WTI','future_oil_contract_1':'Oil Futures','sugar_price_us':'Sugar Price US','ethanol_price_us':'Ethanol Price US','gdp_us':'US GDP','gdp_china':'China GDP','gdp_europe':'Europe GDP','Celsius':'Global Temp'})
merge_result_w_index = merge_result_w_index.rename(columns = {'Gasoline_Price_US': 'Gasoline Price US','co2_conc': 'CO2 Concentration','oil_spot_WTI':'Oil Spot WTI','future_oil_contract_1':'Oil Futures','sugar_price_us':'Sugar Price US','ethanol_price_us':'Ethanol Price US','gdp_us':'US GDP','gdp_china':'China GDP','gdp_europe':'Europe GDP','Celsius':'Global Temp'})
merge_result_w_index
#Weekly Gasoline price problem
from matplotlib.ticker import FormatStrFormatter
from pandas.plotting import scatter_matrix
%matplotlib inline
plt.style.use("default")
plot_fig = main_frame_granger_week_diff.rename(columns = {'Gasoline_Price_US': 'Gasoline Price US return','co2_conc': 'Change in CO2 Concentration','oil_spot_WTI':'Oil Spot WTI return','future_oil_contract_1':'Oil Futures return','sugar_price_us':'Sugar Price US return','ethanol_price_us':'Ethanol Price US return','gdp_us':'Change in US GDP','gdp_china':'Change in China GDP','gdp_europe':'Change in Europe GDP','Celsius':'Change in Global Temp'})
plot_fig1 = plot_fig[['Gasoline Price US return','Oil Spot WTI return','Oil Futures return','Sugar Price US return','Ethanol Price US return']]
scatter_matrix = pd.plotting.scatter_matrix(plot_fig1, alpha=0.2, marker = 'o', figsize = (8,8),diagonal = 'kde')
for ax in scatter_matrix.ravel():
ax.set_xlabel(ax.get_xlabel(), fontsize = 10, rotation = 0)
ax.set_ylabel(ax.get_ylabel(), fontsize = 10, rotation = 90)
ax.yaxis.set_major_formatter(FormatStrFormatter('%.1f'))
plt.suptitle('Weekly correlation plot - change in Gasoline', size = 12, weight = 'bold',va = 'bottom', x= 0.5, y= 0.9)
plt.show()
#Weekly CO2 problem
plot_fig2 = plot_fig[['Change in CO2 Concentration', 'Oil Spot WTI return','Change in US GDP', 'Change in China GDP', 'Change in Europe GDP', 'Change in Global Temp']]
scatter_matrix = pd.plotting.scatter_matrix(plot_fig2, alpha=0.2, marker = 'o', figsize = (8,8),diagonal = 'kde')
for ax in scatter_matrix.ravel():
ax.set_xlabel(ax.get_xlabel(), fontsize = 10, rotation = 0)
ax.set_ylabel(ax.get_ylabel(), fontsize = 10, rotation = 90)
ax.yaxis.set_major_formatter(FormatStrFormatter('%.1f'))
plt.suptitle('Weekly correlation plot - change Carbon Dioxide Concentration', size = 12, weight = 'bold',va = 'bottom', x= 0.5, y= 0.9)
plt.show()
merge_result_w_index.columns
from pandas.plotting import scatter_matrix
import pandas as pd
from sklearn import datasets
color_wheel = {1: "#0392cf", #b
2: "#7bc043", #g
3: "#ee4035"} #r
colors = merge_result_w_index.index.map(lambda x: color_wheel.get(x + 1))
ax = scatter_matrix(merge_result_w_index[['Gasoline Price US','Oil Spot WTI','Oil Futures','Sugar Price US','Ethanol Price US']], color=colors
, alpha=0.5, figsize=(4, 4), diagonal='kde', marker = '.')
for ax in ax.ravel():
ax.set_xlabel(ax.get_xlabel().replace(' ', '\n'), fontsize = 7, rotation = 0)
ax.set_ylabel(ax.get_ylabel().replace(' ', '\n'), fontsize = 7, rotation = 90)
ax.yaxis.set_major_formatter(FormatStrFormatter('%.1f'))
ax.yaxis.set_ticks([])
ax.xaxis.set_ticks([])
plt.suptitle('Correlation plot - change in Gasoline', size = 12, weight = 'bold',va = 'bottom', x= 0.5, y= 0.93)
handles = [plt.plot([],[],color=plt.cm.brg(i/2.), ls="", marker="o", \
markersize=np.sqrt(10))[0] for i in range(3)]
handles = [handles[0],handles[2],handles[1]]
labels=["Weekly", "Monthly", "Quarterly"]
plt.legend(handles, labels, loc=(1.02,0))
plt.show()
from pandas.plotting import scatter_matrix
import pandas as pd
from sklearn import datasets
color_wheel = {1: "#0392cf", #b
2: "#7bc043", #g
3: "#ee4035"} #r
colors = merge_result_w_index.index.map(lambda x: color_wheel.get(x + 1))
ax = scatter_matrix(merge_result_w_index[['Gasoline Price US','Oil Spot WTI','Oil Futures','Sugar Price US','Ethanol Price US']], color=colors
, alpha=0.5, figsize=(4, 4), diagonal='kde', marker = '.')
for i in range(np.shape(ax)[0]):
for j in range(np.shape(ax)[1]):
if i < j:
ax[i,j].set_visible(False)
for ax in ax.ravel():
ax.set_xlabel(ax.get_xlabel().replace(' ', '\n'), fontsize = 7, rotation = 0, weight = 'medium')
ax.set_ylabel(ax.get_ylabel().replace(' ', '\n'), fontsize = 7, rotation = 90, weight = 'medium')
ax.yaxis.set_major_formatter(FormatStrFormatter('%.1f'))
ax.xaxis.set_major_formatter(FormatStrFormatter('%.1f'))
ax.yaxis.set_ticks([])
ax.xaxis.set_ticks([])
plt.suptitle('Correlation plot of change in \n variables VS change in Gasoline', size = 12, weight = 'bold',va = 'bottom', x= 0.5, y= 0.93)
handles = [plt.plot([],[],color=plt.cm.brg(i/2.), ls="", marker="o", \
markersize=np.sqrt(10))[0] for i in range(3)]
handles = [handles[0],handles[2],handles[1]]
labels=["Weekly", "Monthly", "Quarterly"]
plt.legend(handles, labels, loc=(1.02,0))
plt.show()
ax = scatter_matrix(merge_result_w_index[['Gasoline Price US','Oil Spot WTI','Oil Futures','Sugar Price US','Ethanol Price US']], color=colors
, alpha=0.5, figsize=(4, 4), diagonal='kde', marker = '.')
from pandas.plotting import scatter_matrix
import pandas as pd
from sklearn import datasets
color_wheel = {1: "#0392cf", #b
2: "#7bc043", #g
3: "#ee4035"} #r
colors = merge_result_w_index.index.map(lambda x: color_wheel.get(x + 1))
ax = scatter_matrix(merge_result_w_index[['CO2 Concentration', 'Oil Spot WTI','US GDP', 'China GDP', 'Europe GDP', 'Global Temp']], color=colors
, alpha=0.5, figsize=(4, 4), diagonal='kde', marker = '.')
for i in range(np.shape(ax)[0]):
for j in range(np.shape(ax)[1]):
if i < j:
ax[i,j].set_visible(False)
for ax in ax.ravel():
ax.set_xlabel(ax.get_xlabel().replace(' ', '\n'), fontsize = 7, rotation = 0, weight = 'medium')
ax.set_ylabel(ax.get_ylabel().replace(' ', '\n'), fontsize = 7, rotation = 90, weight = 'medium')
ax.yaxis.set_major_formatter(FormatStrFormatter('%.1f'))
ax.yaxis.set_ticks([])
ax.xaxis.set_ticks([])
plt.suptitle('Correlation plot of change in \n variables VS CO2 Concentration', size = 12, weight = 'bold',va = 'bottom', x= 0.5, y= 0.93)
handles = [plt.plot([],[],color=plt.cm.brg(i/2.), ls="", marker="o", \
markersize=np.sqrt(10))[0] for i in range(3)]
handles = [handles[0],handles[2],handles[1]]
labels=["Weekly", "Monthly", "Quarterly"]
plt.legend(handles, labels, loc=(1.02,0))
plt.show()
from pandas.plotting import scatter_matrix
import pandas as pd
from sklearn import datasets
color_wheel = {1: "#0392cf", #b
2: "#7bc043", #g
3: "#ee4035"} #r
colors = merge_result_w_index.index.map(lambda x: color_wheel.get(x + 1))
ax = scatter_matrix(merge_result_w_index[['CO2 Concentration', 'Oil Spot WTI','US GDP', 'China GDP', 'Europe GDP']], color=colors
, alpha=0.5, figsize=(4, 4), diagonal='kde', marker = '.')
for ax in ax.ravel():
ax.set_xlabel(ax.get_xlabel().replace(' ', '\n'), fontsize = 7, rotation = 0)
ax.set_ylabel(ax.get_ylabel().replace(' ', '\n'), fontsize = 7, rotation = 90)
ax.yaxis.set_major_formatter(FormatStrFormatter('%.1f'))
ax.xaxis.set_major_formatter(FormatStrFormatter('%.1f'))
plt.suptitle('Correlation plot - change in CO2 Concentration', size = 12, weight = 'bold',va = 'bottom', x= 0.5, y= 0.93)
handles = [plt.plot([],[],color=plt.cm.brg(i/2.), ls="", marker="o", \
markersize=np.sqrt(10))[0] for i in range(3)]
handles = [handles[0],handles[2],handles[1]]
labels=["Weekly", "Monthly", "Quarterly"]
plt.legend(handles, labels, loc=(1.02,0))
plt.show()
main_frame_granger.columns
main_frame_granger_week = main_frame_granger.groupby('Weekindex').last()[[ 'Date','Gasoline_Price_US','co2_conc','oil_spot_WTI', 'oil_spot_Brent', 'future_oil_contract_1','sugar_price_us', 'ethanol_price_us','gdp_us', 'gdp_china', 'gdp_europe', 'Celsius']]
main_frame_granger_month = main_frame_granger.groupby('Par_Month').last()[[ 'Date','Gasoline_Price_US','co2_conc','oil_spot_WTI', 'oil_spot_Brent', 'future_oil_contract_1','sugar_price_us', 'ethanol_price_us','gdp_us', 'gdp_china', 'gdp_europe', 'Celsius']]
#Quarterly
Par_Quarter = []
for i in range(len(main_frame_granger)):
Par_Quarter.append(str(main_frame_granger.loc[i].Year) + str(main_frame_granger.loc[i].Quarter))
main_frame_granger['Par_Quarter'] = Par_Quarter
main_frame_granger_quarter = main_frame_granger.groupby('Par_Quarter').last()[[ 'Date','Gasoline_Price_US','co2_conc','oil_spot_WTI', 'oil_spot_Brent', 'future_oil_contract_1','sugar_price_us', 'ethanol_price_us','gdp_us', 'gdp_china', 'gdp_europe', 'Celsius']]
main_frame_granger_week.to_csv('weeklygg.csv')
main_frame_granger_month.to_csv('monthlygg.csv')
main_frame_granger_quarter.to_csv('quarterlygg.csv')